Questions tagged [ofstream]

Output file streams are C++ standard library objects used to write to files.

The C++ standard library type std::ofstream is a specialized std::ostream which uses a std::filebuf to write to a file. std::ofstream is declared in fstream.

Use this tag for questions about std::ofstream or the wide-character version std::wofstream, or the class template std::basic_ofstream. For questions about file I/O in C++ the tags , , and might be relevant. For more general questions about output streams use the or tags.

1006 questions
17
votes
7 answers

How to easily indent output to ofstream?

Is there an easy way to indent the output going to an ofstream object? I have a C++ character array that is null terminate and includes newlines. I'd like to output this to the stream but indent each line with two spaces. Is there an easy way to…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
16
votes
2 answers

ifstream and ofstream or fstream using in and out

When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream…
Ishaan
  • 707
  • 1
  • 7
  • 16
16
votes
2 answers

Does ofstream close its files automatically?

Possible Duplicate: do I need to close a std::fstream? int main() { ofstream a("a.txt"); a << "A" << endl; //a.close(); } This works fine, but isn't it necessary to close the file at the end of the program?
nhtrnm
  • 1,411
  • 3
  • 15
  • 24
15
votes
4 answers

c++ std::ofstream flush() but not close()

I'm on MacOSX. In the logger part of my application, I'm dumping data to a file. suppose I have a globally declared std::ofstream outFile("log"); and in my logging code I have: outFile << "......." ; outFile.flush(); Now, suppose my code crashes…
anon
  • 41,035
  • 53
  • 197
  • 293
14
votes
7 answers

Assigning cout to a variable name

In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested)…
user12576
  • 378
  • 1
  • 2
  • 9
14
votes
4 answers

Returning ifstream in a function

Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
13
votes
3 answers

Can you safely close a file that was never opened?

If I have a std::ofstream that may or may not have been opened, is it safe to try to close regardless? In otherwords does close() do anything nasty (throw exception, etc) if !is_open(). For example std::ofstream out; if (some_condition) { …
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
13
votes
5 answers

ofstream doesn't flush

I have the following code, running on Suse 10.1 / G++ 4.1.0, and it doesn't write to the file: #include #include int main(){ std::ofstream file("file.out"); file << "Hello world"; } The file is correctly created and…
kinslayer_e
  • 165
  • 1
  • 1
  • 10
13
votes
2 answers

C++ ostream and ofstream conversions

My code has an ostream object that is accumulated by various modules and ultimately displayed to the console. I'd like ALSO to write this ostreamobject to a file, but do I have to rewrite all of that code using an ofstream object instead, or is…
Tom
  • 133
  • 1
  • 1
  • 4
13
votes
3 answers

ofstream exception handling

Deliberately I'm having this method which writes into a file, so I tried to handle the exception of the possiblity that I'm writing into a closed file: void printMe(ofstream& file) { try { file <<…
Joy
  • 1,707
  • 7
  • 29
  • 44
12
votes
2 answers

Create a vector of ofstreams

I'm trying to create a vector of ofstreams.. vector streams; for (int i = 0; i < numStreams; i++){ ofstream out; string fileName = "text" + to_string(i) + ".txt"; output.open(fileName.c_str()); streams.push_back(out); } This code…
user3298872
  • 231
  • 2
  • 10
11
votes
4 answers

How can I make an ostream reference an ofstream? (C++)

I'm trying to make a simple logger class, and I want the ability to either log to either a generic ostream (cout/cerr) or a file. The design I have in mind is to allow the constructor to either take an ostream& or a filename, and in the latter case…
user1163857
11
votes
1 answer

Why does std::ofstream truncate without std::ios_base::trunc?

According to this C++ reference: http://www.cplusplus.com/reference/fstream/ofstream/ofstream/, the default open mode for std::ofstream is ios_base::out and it mentions no implict other modes. Therefore I would expect that if I overwrite a large…
Benjamin Bihler
  • 1,612
  • 11
  • 32
11
votes
2 answers

Defining fstream inside a 'if' conditional

In an answer there was the following code: if (std::ifstream input("input_file.txt")) ; Which seems convenient, limiting the scope of the 'input' variable to where it's confirmed to be valid, however neither VS2015 nor g++ seems to compile it. Is…
user3358771
  • 297
  • 2
  • 10
11
votes
4 answers

Check if ostream object is cout or ofstream, c++

Is there a way in C++ to check if an ostream object is cout or a ofstream object? Something like: ostream& output(ostream& out) { if (out == cout) return out; else { out << "something different because its not going to…
Jordan
  • 4,928
  • 4
  • 26
  • 39
1
2
3
67 68