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

How to write to middle of a file in C++?

I think this should be quite simple, but my googling didn't help so far... I need to write to an existing file in C++, but not necessarily at the end of the file. I know that when I just want to append text to my file, I can pass the flag ios:app…
nburk
  • 22,409
  • 18
  • 87
  • 132
10
votes
2 answers

Is std::ofstream movable?

I have this map which compiles fine in MSVC10 : std::map m_logFiles; But on ubuntu using g++ 4.5 with C++0x enabled, I get the following error message : /usr/include/c++/4.5/bits/ios_base.h|785|error:…
Jonathan Merlet
  • 275
  • 3
  • 13
10
votes
1 answer

Why fstream is not inherited from ifstream and ofstream in c++?

ifstream and ofstream is for input and output in file, and fstream can do task of both them but not inherited from either of ifstream and ofstream, is this code repetition or something else?
Onk_r
  • 836
  • 4
  • 21
10
votes
2 answers

C++ - How to have multiple threads write to a file

I am currently writing a c++ program that uses threads to write strings to a file. I am using ofstream to write these strings, and I noticed that only one of the threads has access to the file. So my question: Is there any way to use ofstream in…
Flare Cat
  • 591
  • 2
  • 12
  • 24
10
votes
3 answers

Why 'ifstream' and 'ofstream' are added to "std", while 'fstream' can serve both the purposes?

Using std::fstream one can declare objects of both the types ifstream and ofstream. The only difference is that, with fstream we need to provide in, out, app as a parameter which may not always require for other two. Is there anything special about…
iammilind
  • 68,093
  • 33
  • 169
  • 336
9
votes
13 answers

Write a circular file in c++

I need to write a circular file in c++. The program has to write lines in a file and when the code reaches a maximum number of lines, it must overwrite the lines in the beginning of the file. Anyone have any idea?
Kram
  • 113
  • 1
  • 1
  • 6
9
votes
5 answers

Very surprising perfs of fprintf vs std::ofstream (fprintf is very slow)

I was running some benchmarks to find the most efficient way to write a huge array to a file in C++ (more than 1Go in ASCII). So I compared std::ofstream with fprintf (see the switch I used below) case 0: { std::ofstream out(title,…
Vincent
  • 57,703
  • 61
  • 205
  • 388
9
votes
2 answers

no matching function for call to `std::basic_ofstream >::basic_ofstream(std::string&)'

I am trying to write a program that ask the user for a file name and then it opens that file. When I compile it I get the following error: no matching function for call to std::basic_ofstream
ToM MaC
  • 93
  • 1
  • 1
  • 3
9
votes
2 answers

std::ofstream writes \r even in binary mode

I am trying to write a UTF-16 encoded file using std::ofstream(). Even in binary mode writing "\n\0" is written as "\r\n\0". Sample code: std::string filename = ... std::ofstream fout(filename, std::ios_base::binary); fout.write("\xff\xfe",…
Jason
  • 389
  • 2
  • 6
9
votes
3 answers

std::ofstream::write adds characters

I am trying to write binary file using std::ofstream::write method. I found out, that some characters are not written as they are, for example: std::ofstream in("testout"); int i =10; in.write((const char *)(&i), sizeof(i)); in.close(); return…
coffee
  • 703
  • 2
  • 9
  • 14
8
votes
2 answers

C++ ofstream line break

This is my code: #include #include using namespace std; int main() { ifstream ifile ("input.dat", ios::in); ofstream ofile ("output.dat",ios::out); int num; ifile >> num; ofile << num; ofile << endl; …
Pablo Canseco
  • 554
  • 1
  • 10
  • 24
8
votes
3 answers

ofstream reset precision

I'm using c++ to manipulate txt files. I need to write some numbers with a certain precision so I'm doing: ofstrem file; file.open(filename, ios::app); file.precision(6); file.setf(ios::fixed, ios::floafield); //writing number on…
andrea
  • 1,326
  • 7
  • 31
  • 60
8
votes
2 answers

`ofstream` compared to 0

I'm upgrading existing old code to use VS 2019*, in the code I have the following function that fails on the return line: int foo(const char *fn) const { ofstream out(fn,ios::binary); if (out) { //... } return…
SIMEL
  • 8,745
  • 28
  • 84
  • 130
8
votes
1 answer

Why does ofstream insert a 0x0D byte before 0x0A?

I'm outputing an array of unsigned characters in C++ using ofstream fout("filename"); but it produces a spurious character in between. This is the part of the code that makes the problem: for(int i = 0; i < 12; i++) fout << DChuffTable[i]; and this…
raven
  • 775
  • 6
  • 22
8
votes
1 answer

Difference between fstream, ofstream, ostream, iostream

I'm taking a quiz on an intro level class in C++ and I'm trying to understand a question. After searching the internet and not getting an answer, so here I am. Which of the following function declarations will accept either cout or a file stream…
1 2
3
67 68