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

Read file line by line using ifstream in C++

The contents of file.txt are: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 Where 5 3 is a coordinate pair. How do I process this data line by line in C++? I am able to get the first line, but how do I get the next line of the file? ifstream myfile; myfile.open…
dukevin
  • 22,384
  • 36
  • 82
  • 111
85
votes
3 answers

do I need to close a std::fstream?

Possible Duplicate: Do I need to manually close a ifstream? Do I need to call fstream.close() or is fstream a proper RAII object that closes the stream on destruction? I have a local std::ofstream object inside a method. Can I assume that the…
Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
66
votes
4 answers

Writing stringstream contents into ofstream

I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; …
Eric
  • 19,525
  • 19
  • 84
  • 147
58
votes
6 answers

Fast way to write data from a std::vector to a text file

I currently write a set of doubles from a vector to a text file like this: std::ofstream fout; fout.open("vector.txt"); for (l = 0; l < vector.size(); l++) fout << std::setprecision(10) << vector.at(l) << std::endl; fout.close(); But this is…
Diego Fernando Pava
  • 899
  • 3
  • 11
  • 24
51
votes
1 answer

"Incomplete type not allowed " when creating std::ofstream objects

Visual Studio Throws this Strange Error: Incomplete type not allowed When I try to create an std::ofstream object. Here is the code I wrote inside a function. void OutPutLog() { std::ofstream outFile("Log.txt"); } whenever it encounters…
arun49 vs
  • 549
  • 1
  • 4
  • 4
39
votes
3 answers

"ofstream" as function argument

Is there a way to pass output stream as argument like void foo (std::ofstream dumFile) {} I tried that but it gave error : class "std::basic_ofstream>" has no suitable copy constructor
Shibli
  • 5,879
  • 13
  • 62
  • 126
38
votes
6 answers

std::ofstream, check if file exists before writing

I am implementing file saving functionality within a Qt application using C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream…
cweston
  • 11,297
  • 19
  • 82
  • 107
26
votes
4 answers

How to do fsync on an ofstream?

I want to make sure that an ofstream has been written to the disk device. What's the portable way (portable on POSIX systems) of doing this? Does that solve the problem if I open the file separately in read-only append mode to get a file descriptor…
Vik
  • 495
  • 1
  • 5
  • 11
25
votes
2 answers

Is it possible to pass cout or fout to a function?

I'm trying to find a way to pass fout or cout to a function. I realize there are logically easy ways to deal with this, like put ifs in any function that outputs data or even just write the function both ways. However, that seems primitive and…
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
24
votes
3 answers

Why is `is_open()` non-const?

I have a function similar to below which is const and needs to check that a file stream is open prior to continuing: bool MyClass::checkSomeStuff() const { // Where outputFile_ is a std::ofstream if ( ! outputFile_.is_open() ) { …
Component 10
  • 10,247
  • 7
  • 47
  • 64
21
votes
4 answers

Why does ostream::write() require ‘const char_type*’ instead of ‘const void*’ in C++?

The fwrite() function in C uses const void *restrict buffer as the first argument, so you can pass pointer to your struct as the first parameter directly. http://en.cppreference.com/w/c/io/fwrite e.g. fwrite(&someStruct, sizeof(someStruct), 1,…
Mr. Ree
  • 871
  • 9
  • 20
20
votes
2 answers

Error handling in std::ofstream while writing data

I have a small program where i initialize a string and write to a file stream: #include #include using namespace std; int main() { std::ofstream ofs(file.c_str()); string s="Hello how are you"; if(ofs) ofs<
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51
18
votes
2 answers

c++ - fstream and ofstream

What is the difference between: fstream texfile; textfile.open("Test.txt"); and ofstream textfile; textfile.open("Test.txt"); Are their function the same?
Robin
  • 5,366
  • 17
  • 57
  • 87
17
votes
2 answers

Avoid contents of an existing file to be overwritten when writing to a file

I am trying to make a game that implements high scores into a .txt file. The question I have is this : when I make a statement such as: ofstream fout("filename.txt"); Does this create a file with that name, or just look for a file with that name?…
Monkeyanator
  • 1,346
  • 2
  • 14
  • 29
17
votes
7 answers

How does one write the hex values of a char in ASCII to a text file?

Here is what I currently have so far: void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix ) { unsigned char *buf = (unsigned char*)ptr; for( int i = 0; i < buflen; ++i ) { if( i % 16 == 0 ) { …
xian
  • 4,657
  • 5
  • 34
  • 38
1
2 3
67 68