Questions tagged [fstream]

fstream provides an iostream interface for file I/O in C++.

The C++ standard library type std::fstream is an iostream type for reading and writing files.

The objects of this class internally maintain a pointer to a std::filebuf object (derived from std::streambuf) that can be obtained/modified by calling the rdbuf member function. The filebuf transfers characters to/from a file and performs any necessary conversion between the on-disk format and the in-memory representation.

For questions specific to fstream use this tag or , , or for more general questions use , , or .

2840 questions
25
votes
1 answer

Does std::ofstream truncate or append by default?

If you call the std::ofstream constructor without openmode flags, the default flag is ios_base::out. But does this imply ios_base::trunc or ios_base::app? In other words, if you already have a non-empty file "past.txt" in your file system and you…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
25
votes
6 answers

CLion C++ can't read/open .txt file in project directory

I have a .txt file in my project directory that I made and populated with data. directory structure looks like: /Users/asd/ClionProjects/ProjectWithTemplates/ main.cpp cmake twoday.txt here is my code: #include #include #include…
Rekumaru
  • 421
  • 1
  • 4
  • 10
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
22
votes
3 answers

Why is failbit set when eof is found on read?

I've read that predates . Ignoring the fact that exceptions on fstream aren't very informative, I have the following question: It's possible to enable exceptions on file streams using the exceptions() method. ifstream…
ceztko
  • 14,736
  • 5
  • 58
  • 73
22
votes
1 answer

How to append to a file with fstream fstream::app flag seems not to work

i simply want to write (append) to a logfile. I looked it up here: http://www.cplusplus.com/reference/iostream/fstream/open/ so this is what i did #include fstream outfile; //outfile.open("/tmp/debug.txt" ); // works, simply for…
groovehunter
  • 3,050
  • 7
  • 26
  • 38
22
votes
1 answer

attempting to reference a deleted function

I'm trying to learn about the fstream class and I'm having some trouble. I created a couple of txt files, one with a joke and the other with a punchline (joke.txt) and (punchline.txt) just for the sake of reading in and displaying content. I ask the…
jacksonSD
  • 677
  • 2
  • 13
  • 27
21
votes
2 answers

Why does GCC's ifstream >> double allocate so much memory?

I need to read a series of numbers from a space-separated human-readable file and do some math, but I've run into some truly bizarre memory behavior just reading the file. If I read the numbers and immediately discard them... #include int…
Dan
  • 4,312
  • 16
  • 28
21
votes
1 answer

Why can't I move std::ofstream?

Looking at previous answers on SO, it seems that while std::ostream is not be movable, std::ofstream should be. However, this code #include int main() { std::ofstream ofs; std::ofstream ofs2{std::move(ofs)}; } does not seem to…
toth
  • 2,519
  • 1
  • 15
  • 23
20
votes
2 answers

Read 32-bit integer from binary file in C++?

My binary file looks like this. 00000000: 0000 0803 0000 ea60 0000 001c 0000 001c 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 left column is address. I just tried to read 0000 0803(=2051) as follows ifstream if; if.open("file"); uint32_t…
plhn
  • 5,017
  • 4
  • 47
  • 47
20
votes
2 answers

Reading and writing to the same file using the same fstream

I have a file that already contains some data (say, 8 kB). I want to read something from the beginning of the file, and then overwrite data starting where I finished reading. So I try to use the following code: std::fstream stream("filename",…
svick
  • 236,525
  • 50
  • 385
  • 514
20
votes
4 answers

Retrieving file descriptor from a std::fstream

Possible Duplicate: Getting a FILE* from a std::fstream I am working on Linux and file descriptors are the main model in this OS. I was wondering whether is there any library or any way to retrieve the native Linux file descriptor starting from…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
19
votes
1 answer

Writing integer to binary file using C++?

I have a very simple question, which happens to be hard for me since this is the first time I tried working with binary files, and I don't quite understand them. All I want to do is write an integer to a binary file. Here is how I did it: #include…
eqagunn
  • 221
  • 1
  • 4
  • 11
19
votes
3 answers

How do I print out the contents of a file? C++ File Stream

I am using fstream and C++ and all I want my program to do is to print out to the terminal the contents of my .txt file. It may be simple, but I have looked at many things on the web and I can't find anything that will help me. How can I do this?…
Lucas Smith
  • 698
  • 2
  • 9
  • 17
19
votes
2 answers

Print integer with thousands and millions separator

Got a question about printing Integers with thousands/millions separator. I got a Textfile where i got Country, City,Total Population. I have to read in the File, and sort by country. If country is eual, i have to sort descending by…
d0zer
  • 235
  • 1
  • 4
  • 9
19
votes
3 answers

C++ Program in Xcode not outputting simple text file using outFile

I am running a program using this simple example code to output a text file. I am using Xcode and simply started a new C++ project from command line tools. For some reason the program does not output any file onto my Mac. Please help figure out why…
Nearpoint
  • 7,202
  • 13
  • 46
  • 74