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
5
votes
3 answers

Reading chunks from a file in binary mode to a buffer and writing that buffer to another file

I am trying to achieve something like this: while (ifstream has not been entirely read) { read a chunk of data into a buffer that has size BUFLEN write this buffer to ostream } At first I tried to achieve this by using ifstream.eof() as my…
codd
  • 612
  • 1
  • 7
  • 15
5
votes
2 answers

Problem with ostream/ofstream inheritance

I'm writing a C++ program and I need some help understanding an error. By default, my program prints to the terminal (STDOUT). However, if the user provides a filename, the program will print to that file. If I'm writing to the terminal, I will use…
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
5
votes
3 answers

Open an ofstream with tilde (~) in path name

I have to open some file for writing, and its name contains the tilde sign (~). The following code fails to create the desired text file. If I replace the ~ with /home/oren then everything works fine. #include #include const…
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
5
votes
2 answers

Write a vector of cv::Mat to binary file in c++

As in the title, I have std::vector matrices which I want to write/read to/from binary file. Now, following this answer, all I should do is for writing is: ofstream fout("matrices.bin", ios::out | ios::binary); size_t size =…
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
5
votes
0 answers

C++ text file content erased after power loss

I am logging some data into a .txt file during a program execution. The problem is that sometimes I need to cut the power to the system, but I would still like to save the written data. Here is the code I use to write on the txt file: std::ofstream…
charles
  • 713
  • 1
  • 6
  • 16
5
votes
1 answer

C++ ifstream, ofstream: What's the difference between raw read()/write() calls and opening file in binary mode?

This question concerns the behaviour of ifstream and ofstream when reading and writing data to files. From reading around stackoverflow.com I have managed to find out that operator<< (stream insertion operator) converts objects such as doubles to…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
5
votes
1 answer

Is there a way to create a common output stream object to print on the console and to a file in c++?

I am writing a code where I have to print the same data on the console and to a file. Is there a way to populate a common output stream object and then display it on console using cout and export it to a file, using fstream and iostream libraries?
5
votes
1 answer

difference between << s.str() and << s.rdbuf()

Can someone explain the subtle difference in: ofstream f("test.txt") std::stringstream s; s<<""; f << s.rdbuf(); f.good() // filestream is bad!! ofstream f("test.txt") std::stringstream s; s<<""; f << s.str(); f.good() // is still ok! I mostly…
Gabriel
  • 8,990
  • 6
  • 57
  • 101
5
votes
1 answer

C++ std::stringstream/ostringstream and UTF characters

I'm writing a program which processes some data, outputs it to a .csv file, then writes a GNUplot script, and calls GNUplot to execute the script and create an image file all with the same name (only different extensions). The filenames contain UTF…
pavichokche
  • 91
  • 1
  • 8
5
votes
2 answers

How to close ofstream after assigning to ostream?

I can do std::ostream& out = condition ? std::cout : std::ofstream(filename); but how do I close in case of out = std::ofstream(filename)?
downforme
  • 371
  • 2
  • 15
5
votes
4 answers

C++ find size of ofstream

I have code that currently does something like the following: ofstream fout; fout.open("file.txt"); fout<<"blah blah "<<100<<","<<3.14; //get ofstream length here fout<<"write more stuff"<
user788171
  • 16,753
  • 40
  • 98
  • 125
5
votes
5 answers

Writing at the beginning of a file, keeping file contents

I have a text file where I want to write. I want to keep the file content always. I want to write following a "FIFO" (last write always on the top line of the file). I try using fout.open("filename"); with ate mode to keep file content, after that…
Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77
5
votes
2 answers

Writing a lot of txt files (90), 5MB per file, takes up to 1400s

I'm a student in Electrical Engineering. As an assigment I need to implement the back projection algorithm used in medical imaging to form in image. To calculate the final image a lot information is calculated and kept in a vector. At a certain…
pivu0
  • 138
  • 1
  • 10
5
votes
2 answers

vector of ofstream pointers

Here is my problem, I need to create X number of files and write to them depending on different factos, my solution was to create a vector of ofstream pointers like this #include #include…
Jorge Kageyama
  • 393
  • 4
  • 17
4
votes
4 answers

Basic Input/Output C++ Error

All I want is a c++ program that will read in a txt file, put each row in an array, then print a duplicate copy into another txt file. Here's my code... #include #include #include using namespace std; int main…