Questions tagged [ifstream]

ifstream provides an interface to read data from files as input streams.

The C++ standard library type std::ifstream is a specialized std::istream which uses a std::filebuf to read from a file.

Use this tag for questions about std::ifstream or the wide-character version std::wifstream (also ), or the class template std::basic_ifstream. For questions about file I/O in C++ the tags , , and might be relevant. For more general questions about input streams use the or tags.

2140 questions
12
votes
2 answers

C++ std::ifstream: check if characters are left to read

Is there a way to check if any characters are left in an ifstream to read and if yes, how can I do this. If you know for sure that this isn't possible, please tell me so.
FSMaxB
  • 2,280
  • 3
  • 22
  • 41
12
votes
5 answers

c++ use ifstream from memory

I have some code that uses ifstream to read some data from a file and everything works. Now I wish, without modifying some code, read this data from a memory, actually I have a char * that contains the data... How can I put my char * data into a…
ghiboz
  • 7,863
  • 21
  • 85
  • 131
12
votes
3 answers

C++ How to read in objects with a given offset?

Now I have a file with many data in it. And I know the data I need begins at position (long)x and has a given size sizeof(y) How can I get this data?
shevron
  • 282
  • 2
  • 4
  • 14
11
votes
4 answers

using one ifstream variable for reading several files

Possible Duplicate: C++ can I reuse fstream to open and write multiple files? why is it not possible to use one ifstream variable for opening one file, reading it, then closing it and, after that, opening another file, reading and closing,…
psukys
  • 387
  • 2
  • 6
  • 20
11
votes
2 answers

Defining fstream inside a 'if' conditional

In an answer there was the following code: if (std::ifstream input("input_file.txt")) ; Which seems convenient, limiting the scope of the 'input' variable to where it's confirmed to be valid, however neither VS2015 nor g++ seems to compile it. Is…
user3358771
  • 297
  • 2
  • 10
11
votes
2 answers

Difference between ifstream.good() and bool(ifstream)

I'm writing a program which takes several variables from a text file. When the program finds EOF, it ends entering data. int main() { int val, count = 0; ifstream fileIn; fileIn.open("num.txt"); fileIn >> val; while (fileIn) …
Szymon Bednorz
  • 425
  • 3
  • 10
  • 25
11
votes
1 answer

How to open a file with relative path in C++?

I am writing test cases right now and I created some test files which I try to read. The absolute path is: /home/user/code/Project/source/Project/components/Project/test/file.dat but testing with an absolute path is bad for obvious reasons. So I…
Peter111
  • 803
  • 3
  • 11
  • 21
11
votes
3 answers

how can I read exactly 128 bytes from an fstream into a string object?

How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to…
Southern Hospitality
  • 1,260
  • 1
  • 8
  • 12
11
votes
1 answer

Reading binary file into vector reading less than full file

I have binary files whose contents I'm trying to read into a vector. All the files are the same size, yet using my code below, the final vector size is always a little bit smaller than the file size, and it's different from file to file (but same…
Stan
  • 1,227
  • 12
  • 26
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
11
votes
1 answer

ifstream.eof() - end of file is reached before the real end

I have a roughly 11.1G binary file where stores a series of the depth frames from the Kinect. There are 19437 frames in this file. To read one frame per time, I use ifstream in fstream but it reaches eof before the real end of the file. (I only got…
willSapgreen
  • 1,365
  • 2
  • 17
  • 27
10
votes
2 answers

Using ifstream as fscanf

Assume that I have an input as follows: N (X_1,Y_1) (X_2,Y_2) .... (X_N, Y_N) where N, X_i and Y_i are integers. An example: 2 (55,1) (521,7) To read this, I can do something like this(assume all variables are defined, etc.): fscanf(fin,"%d…
kolistivra
  • 4,229
  • 9
  • 45
  • 58
10
votes
2 answers

Reading binary data into struct with ifstream

I'm trying to read binary data from a file using ifstream. Specifically, I'm trying to populate this "Header" struct with data read from a file: struct Header { char id[16]; int length; int count; }; Now, if I read the file in this…
Dan
  • 1,198
  • 4
  • 17
  • 34
10
votes
1 answer

Is using istream::seekg too much expensive?

In c++, how expensive is it to use the istream::seekg operation? EDIT: How much can I get away with seeking around a file and reading bytes? What about frequency versus magnitude of offset? I have a large file (4GB) that I am parsing, and I want to…
Brian
  • 3,453
  • 2
  • 27
  • 39
10
votes
1 answer

How to read binary file with unicode filename c++?

In the project I'm working on, I deal with quite a few string manipulations; strings are read from binary files along with their encoding (which can be single or double byte). Essentially, I read the string value as vector, read the encoding…
Aleks G
  • 56,435
  • 29
  • 168
  • 265