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
4
votes
4 answers

Writing polymorphic class data to a file?

So I have these classes. There's one base class, but it has/will have lots and lots of derivatives, and those derivative classes will be able to have derivatives as well. I'd like to be able to have a function that writes their binary data to a…
lowq
  • 628
  • 6
  • 18
4
votes
2 answers

fstream ifstream I don't understand how to load a data file into my program

My professor is very smart but expects complete noobs like me to just know how to program c++. I don't understand how the fstream function works. I will have a data file with three columns of data. I will have to determine with a logarithm whether…
lprater1
  • 65
  • 1
  • 1
  • 7
4
votes
5 answers

how to print \" in C++

I need to print a string that says exactly: std::string("-I\"/path/to/dir\" "); Basically, I need to do this because I am using C++ code to generate C++ code. I want to write the above string via an ofstream, so something like ofstream fout; fout…
user788171
  • 16,753
  • 40
  • 98
  • 125
4
votes
0 answers

ofstream fails to open existing file

While working with the OpenXcom app, I noticed that at some point the app stopped updating the options.cfg file. I tracked this issue down to a failure creating an ofstream object for the options.cfg file in the Options::save() method. A search…
James Lent
  • 99
  • 1
  • 4
4
votes
3 answers

What is the difference between ifstream, ofstream and fstream?

In file handling, I came across ifstream, ofstream and fstream. Can anyone tell me the main difference between these?
Tom
  • 85
  • 1
  • 2
  • 6
4
votes
2 answers

Writing to ofstream using ios::ate overwrites existing file

I am trying to append a sequence of matrices to disk as CSV and have found that using ios::ate overwrites any existing file created previously. To illustrate this problem via a simplified model, a second call to the function write_nums() below…
amandini
  • 41
  • 2
4
votes
1 answer

Exporting an array to txt file

I have a code that receives 3 numbers from the user, then sorts them and prints out a sorted array. I'm trying to export the sorted numbers into a .txt file but all I get is some random number like "342142464" in the txt file. I fail to understand…
Mr. B
  • 43
  • 5
4
votes
0 answers

C++ try/catch block does not catch exception of given type

I just encountered a case where an exception of type std::ios_base::failure was thrown inside a try block that catches this type of exception, but it does not get caught. Minimal working example #include #include int main() { …
sasquires
  • 356
  • 3
  • 15
4
votes
1 answer

dupplicating std::ofstream appended content

I am using a std::ofstream for trace output. For some reasons, I sometimes want to dupplicate what I have appended at the end of the std::ofstream (that is not flushed or closed yet), into another std::ofstream; Do you think of any way of achieving…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
4
votes
2 answers

How do I find the directory path of a file I created from ofstream in C++?

I want to get the directory of a file which I created using ofstream. Code I wrote is something like std::ofstream txt("sample.txt", std::ios::binary); Then I write some things to the txt file, then call txt.close() to close the ofstream. While…
js0823
  • 1,843
  • 8
  • 24
  • 36
4
votes
1 answer

Can "eof" be set in ofstream?

I could not find pretty much any information on this. Whether and if so, under what circumstances, can eofbit be set (meaning ofstream_instance.eof() is true )? I am more interested in an independent ofstream, one that is not associated with an…
TStancek
  • 310
  • 1
  • 12
4
votes
1 answer

reading multiple lines from .txt file as string removing white spaces and creating new file for output

I am trying to write a program that reads information from a .txt file, removes the unneeded spaces between the words/parts and saves the outcome to a new output .txt file. I have managed to get most of this working after looking through a lot of…
4
votes
5 answers

C++ Writing to file and Console Output at the same time with simple code

I am trying to write the following entire function to a text file while still maintaining its console output functionality without having code redundancy. Is there a simple way to post an entire method's result to a file and console at the same…
4
votes
0 answers

Does fstream::flush() or fstream::sync() guarantee the data have been written to the disk?

I'm writing a mini database by using C++. In log module, I use fstream to append data to log file and I need data to be immediately written to disk. In http://www.cplusplus.com/doc/tutorial/files/, I find When the buffer is flushed, all the data…
梁宇坤
  • 66
  • 4
4
votes
1 answer

Overwrite an existing text file c++

This is how my Save As works - it is copying the current file's lines until it reaches the first figure and then I use my print methods to print the figure's info and then close the tag. std::ofstream newFile(filePath1_fixed,…
colos_enough
  • 164
  • 1
  • 1
  • 9