tags:

views:

5201

answers:

5

I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.

Can anyone help me?

+15  A: 

One way to do this is to create an instance of the ofstream class, and use it to write to your file. Here's a link to a website that has some example code, and some more information about the standard tools available with most implementations of C++:

ofstream reference

For completeness, here's some example code:

// using ofstream constructors.
#include <iostream>
#include <fstream>  

std::ofstream outfile ("test.txt");

outfile << "my text here!" << std::endl;

outfile.close();

You want to use std::endl to end your lines. An alternative is using '\n' character. These two things are different, std::endl flushes the buffer and writes your output immediately while '\n' allows the outfile to put all of your output into a buffer and maybe write it later.

James Thompson
Not to mention that std::endl also writes the correct platform specific newline character string.
Grant Limberg
To Limberg: thats wrong. "endl: Effects: Calls os.put(os.widen('\n')), then os.flush()" -- C++ standard, 27.6.2.7/1
@Grant Limberg: '\n' also writes the correct platform specific newline--it's translated as appropriate by the ostream internals.
Drew Hall
@James Thompson: The only reason to use std::endl is if you really need to guarantee that the buffer is flushed to the file IMMEDIATELY. Otherwise, it's a needless pessimization (file i/o is very expensive!).
Drew Hall
@Grant Limberg: http://en.wikipedia.org/wiki/Newline#Newline_in_programming_languages
cic
When using ofstream for writing to a file, then no file locking is implemented. It is possible for others to read the half-written file, or for others to open another ofstream and write simultaneously.
Rolf Kristensen
+4  A: 
#include <iostream>
#include <fstream>

int main() {
  std::ofstream o("Hello.txt");

  o << "Hello, World\n" << std::endl;

  return 0;
}
Sean Bright
The \n in the string is redundant when using std::endl.
Greg Hewgill
The return 0; in the function is redundant when using "int main()"
True, but I like to explicitly flush the output buffer.
Sean Bright
The #include <iostream> is redundant when using std::ofstream.
endl does more than flush: it outputs a newline too. So either print \n then std::flush, or just use std::endl.
Thanks sean.bright it works!
Uffo
streams get flushed on destruction, so the std::endl is completely superfluous in this case. However, the "return 0" is definitely NOT redundant. It was a common case in C to imply "return 0", but that is not true with C++.
Tom
@Tom: basic.start.main: "If control reaches the end of main without encountering a return statement, the effect is that of executing: return 0;".
cic
also std::endl is not superfluous. it outputs a '\n' before flushing. omitting it will result in only one trailing newline instead of two. oO
Johannes Schaub - litb
litb no one event wanted 2 newlines, sean thought he was only outputting one
Thanks for all the feedback, both positive and negative. You are all absolutely correct, I included an unnecessary std::endl and #include. My personal preference regarding the return statement is to always include them, even in methods returning void.
Sean Bright
+1 for noting that ofstream is automatically closed when it goes out of scope!
Mr.Ree
A: 

Thanks guys!Solved!

Uffo
In the future, do not post a new "answer" unless you are answering your own question. Secondly, you should mark one of the supplied answers as the answer (in this case, the most complete response is James Thompson's). Read the FAQ for more information.
Sean Bright
+3  A: 

This is a really basic question. If you are serious about C++ programming then why not make a little investment and for example get a copy of 'The C++ Standard Library' by Josuttis. It will answer all your file related questions.

St3fan
A: 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string filename;

int main() {
  std::ofstream o(filename.c_str());

  o << "Hello, World\n" << std::endl;

  return 0;
}

This is what I had to do in order to use a variable for the filename instead of a regular string.

Angelo