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
238
votes
13 answers

mmap() vs. reading blocks

I'm working on a program that will be processing files that could potentially be 100GB or more in size. The files contain sets of variable length records. I've got a first implementation up and running and am now looking towards improving…
jbl
  • 2,710
  • 3
  • 18
  • 13
185
votes
1 answer

C++: variable 'std::ifstream ifs' has initializer but incomplete type

Sorry if this is pretty noobish, but I'm pretty new to C++. I'm trying to open a file and read it using ifstream: vector load_f(string file) { vector text; ifstream ifs(file); string buffer, str_line; int brackets = 0; …
beakr
  • 5,709
  • 11
  • 42
  • 66
130
votes
7 answers

Reading from text file until EOF repeats last line

The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this? Code: #include #include…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
112
votes
8 answers

How to construct a c++ fstream from a POSIX file descriptor?

I'm basically looking for a C++ version of fdopen(). I did a bit of research on this and it is one of those things that seems like it should be easy, but turns out to be very complicated. Am I missing something in this belief (i.e. it really is…
BD at Rivenhill
  • 12,395
  • 10
  • 46
  • 49
104
votes
9 answers

How to read a file line by line or a whole text file at once?

I'm in a tutorial which introduces files (how to read from file and write to file) First of all, this is not a homework, this is just general help I'm seeking. I know how to read one word at a time, but I don't know how to read one line at a time,…
Mody
  • 1,267
  • 3
  • 10
  • 11
98
votes
6 answers

Using C++ filestreams (fstream), how can you determine the size of a file?

I'm sure I've just missed this in the manual, but how do you determine the size of a file (in bytes) using C++'s istream class from the fstream header?
warren
  • 32,620
  • 21
  • 85
  • 124
86
votes
7 answers

C++ Filehandling: Difference between ios::app and ios::ate?

What's the difference between ios::ate and ios:app when writing to a file. In my view, ios::app gives you the ability to move around in the file, whereas with ios::ate it can only read/write at the end of the file. Is this correct?
Adam_G
  • 7,337
  • 20
  • 86
  • 148
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
80
votes
9 answers

Getting a FILE* from a std::fstream

Is there a (cross-platform) way to get a C FILE* handle from a C++ std::fstream ? The reason I ask is because my C++ library accepts fstreams and in one particular function I'd like to use a C library that accepts a FILE*.
Bek
79
votes
3 answers

std::fstream buffering vs manual buffering (why 10x gain with manual buffering)?

I have tested two writing configurations: Fstream buffering: // Initialization const unsigned int length = 8192; char buffer[length]; std::ofstream stream; stream.rdbuf()->pubsetbuf(buffer, length); stream.open("test.dat", std::ios::binary |…
Vincent
  • 57,703
  • 61
  • 205
  • 388
66
votes
8 answers

How to check if a file exists and is readable in C++?

I've got a fstream my_file("test.txt"), but I don't know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that? I use Linux.
Jerry
  • 1,139
  • 3
  • 9
  • 19
62
votes
4 answers

What is a stream in C++?

I have been hearing about streams, more specifically file streams. So what are they? Is it something that has a location in the memory? Is it something that contains data? Is it just a connection between a file and an object?
Mohamed Ahmed Nabil
  • 3,949
  • 8
  • 36
  • 49
61
votes
2 answers

Getting filename (or path) from fstream

Can I get a file name or its path from a fstream object? I looked through the methods of fstream and didn't find anything close to it.
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
55
votes
1 answer

Why am I getting this ifstream error?

Implicit instantiation of undefined template 'std::basic_ifstream>' #ifndef MAPPER_H #define MAPPER_H #include #include #include #include "KeyValue.h" #include "Parser.h" using namespace…
OghmaOsiris
  • 843
  • 2
  • 8
  • 14
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
1
2 3
99 100