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
8
votes
2 answers

Why do I make "use of deleted" function when passing a std::ofstream as parameter?

I have a member that is std::ofstream fBinaryFile and a void setFile( std::ofstream& pBinaryFile ) { fBinaryFile = pBinaryFile; } output: Data.h:86:16: error: use of deleted function ‘std::basic_ofstream&…
user3050386
  • 139
  • 1
  • 9
8
votes
2 answers

C++, regarding fprintf and ofstream

I've been using fprintf for a while now and I'd like to ask a question. What is the equivalent of this fprintf line: fprintf(OutputFile, "%s", "SomeStringValue"); using ofstream ? How to use the "%s" in ofstream is what I'd really like to know. How…
aresz
  • 2,589
  • 6
  • 34
  • 51
8
votes
3 answers

Writing Unicode to a file in C++

I have a problem with writing unicode to a file in C++. I want to write to a file with my own extension a few smiley faces that you can get by typing ALT+NUMPAD(2). I can display it on CMD by making a char and assigning the value of '\2' to it and…
Garrett Ratliff
  • 309
  • 3
  • 5
  • 10
7
votes
2 answers

How can I determine the current size of the file opened by std::ofstream?

I have a class that has a filestream of type ofstream. The constructor opens the file in append mode and all the messages always get written at the end of the file. I need to write into outputFile up to some fixed size say 1Mb, then I need to close,…
void
  • 338
  • 5
  • 19
7
votes
2 answers

Use multiple ofstreams to write to a single output file in c++

I have class Writer that has two ofstream members. Both streams are associated with the same output file. I'd like to use both streams in Writer::write method, but to make sure that each stream writes to the end of the real output file. Code class…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
7
votes
1 answer

Difference between casting ifstream to bool and using ifstream::is_open()

Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
7
votes
1 answer

Change or check the openmode of a std::ofstream

In some code that does a lot of file i/o using std::ofstream, I'm caching the stream for efficiency. However, sometimes I need to change the openmode of the file (e.g. append vs truncate). Here is some similar mock code: class Logger { public: …
Ari
  • 1,102
  • 9
  • 17
7
votes
2 answers

Use of `ofstream` appears not to create nor write to file

At the end of a simulation, I want to write some results as an appended row to a data file. The code I am using is the following, where you can assume that outFile was correctly allocated as an std::ofstream, that output_file is a std::string…
ely
  • 74,674
  • 34
  • 147
  • 228
6
votes
3 answers

C++ - ofstream doesn't output to file until I close the program

I have the following code: ofstream mOutFile.open(logPath, ios_base::app); string lBuilder; lBuilder.append("========================================================\n"); lBuilder.append("Date: "); …
codewario
  • 19,553
  • 20
  • 90
  • 159
6
votes
1 answer

How to check if file copy and writing was successful

I am using C++ 11 to copy a file this way: std::ifstream src(srcPath, std::ios::binary); std::ofstream dst(destinationPath, std::ios::binary); dst << src.rdbuf(); I am creating a new file this way: std::ofstream out(path); out <<…
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
6
votes
1 answer

overloaded operator << on ofstream concatenation problems

I have the following code: struct simple { simple (int a1, int a2) : member1(a1), member2(a2) {} int member1; int member2; }; std::ofstream &operator << (std::ofstream &f, const simple &obj) { f<
abigagli
  • 2,769
  • 4
  • 29
  • 32
6
votes
1 answer

How can I redirect a std::ofstream to std::cout?

I'm working with some code that uses a global debug logger that is of type std::ofstream*. I would like to redirect this to std::cout since I'm using the code in realtime, as opposed to a batch method for which it was designed. Is it possible to…
stix
  • 1,140
  • 13
  • 36
6
votes
1 answer

Why does ofstream::flush() return ostream?

I was trying to do: std::ofstream outFile (fname, std::ios::binary); //... outFile.flush(); outFile.close(); which works fine. But when I try to combine the two lines since flush returns a reference: outFile.flush().close(); It gives error saying:…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
6
votes
1 answer

Format float variable output in c++ using std::ofstream

I have the following code: std::ofstream myfile; std::stringstream filename3; myfile.open("results.txt"); myfile << "precision= " << precision << "\n"; And the output in my file is formatted like this: precision= 5.96e-07... How can I print the…
obelix
  • 880
  • 2
  • 16
  • 43
6
votes
2 answers

Policy based approach with logger

I spend some time with redesigning a logger class I did once into a policy based approach after reading an article about policy based design and wanting to try something myself. Some code: template
Chris
  • 1,226
  • 1
  • 12
  • 26