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
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
10
votes
2 answers

Can I use ifstream in Android NDK to access Assets?

My question is pretty simple, but I am having a hard time finding any info about this online. Is it possible to use ifstream to open a file from assets and/or resources using Android NDK? For example, placing a test.txt file in /assets and trying…
Nitzan Wilnai
  • 923
  • 9
  • 24
9
votes
1 answer

Ifstream open() doesn't set error bits when argument is a directory

In a C++ program, using std::ifstream, I'm attempting to open a user-specified file -- so far so good. However, I accidentally entered a filename that's actually a directory, and I was quite surprised to see that attempting to open() that directory…
SáT
  • 3,633
  • 2
  • 33
  • 51
9
votes
2 answers

ifstream, end of line and move to next line?

how do i detect and move to the next line using std::ifstream? void readData(ifstream& in) { string sz; getline(in, sz); cout << sz <> v; if (in.good()) …
user34537
9
votes
2 answers

Using ifstream to read floats

I'm trying to read a series of floats from a .out file using ifstream, but if I output them afterwards, they are not correct. This is my input code: float x, y, z; ifstream table; table.open("Resources/bones.out"); if (table.fail()) { cout <<…
jsan
  • 1,047
  • 8
  • 20
  • 33
9
votes
1 answer

Why seekg does not work with getline?

Seekg does not seem to work, when I reach EOF in myFile. ifstream myFile("/path/file"); for(int i; i < 10; i++){ myFile.seekg(0);//reset position in myFile while(getline(myFile, line)){ doSomething } } So, now I am opening…
rluks
  • 2,762
  • 8
  • 38
  • 56
9
votes
2 answers

How do I return an ifstream back to the beginning of a line that was just read in C++?

After I read a line from a file using ifstream, is there a way to bring the stream back to the beginning of the line I just read, conditionally? using namespace std; //Some code here ifstream ifs(filename); string line; while(ifs >> line) { …
The Unknown Dev
  • 3,039
  • 4
  • 27
  • 39
8
votes
4 answers

Is ios::in needed for ifstream's opened in binary mode?

What's the difference between these two? Isn't the in flag object thing redundant? Thanks. std::ifstream file1("one.bin", std::ifstream::in | std::ifstream::binary); std::ifstream file2("two.bin", std::ifstream::binary);
8
votes
4 answers

Getting the nth line of a text file in C++

I need to read the nth line of a text file (e.g. textfile.findline(0) would find the first line of the text file loaded with ifstream textfile). Is this possible? I don't need to put the contents of the file in an array/vector, I need to just…
MrJackV
  • 547
  • 3
  • 5
  • 12
8
votes
3 answers

Cannot read simple binary integers from file? (C++)

My code is simply as this: UPDATED: #include #include using namespace std; int main(int argc, char **argv) { ifstream r("foo.bin", ios::binary); ofstream w("foo.bin", ios::binary); int i; int ints[10] =…
John S.
  • 83
  • 1
  • 3
8
votes
5 answers

Reading a Matrix txt file and storing as an array

I'm currently writing a Simulated Annealing code to solve a traveling salesman problem and have run into difficulties with storing and using my read data from a txt file. Each row & column in the file represents each city, with the distance between…
Temperedsoul
  • 127
  • 1
  • 2
  • 8
8
votes
1 answer

C++ How to assign ifstream content (from file) to string with offset (istreambuf_iterator)

i try to read parts of a binary file content into a string. Why a string? I need this for my message protocol (with protobuf). The following works very well: std::string* data = new std::string(); std::ifstream ifs("C:\\data.bin",…
8
votes
1 answer

How to read UTF-8 encoded text file using std::ifstream?

I'm having a hard time to parse an xml file. The file was saved with UTF-8 Encoding. Normal ASCII are read correctly, but Korean characters are not. So I made a simple program to read a UTF-8 text file and print the content. Text…
JaeJun LEE
  • 1,234
  • 3
  • 11
  • 27
8
votes
3 answers

Files in folders not found in iOS app using C++

I'm trying to read files stored in assets folder and its subfolders using std::ifstream in an iOS app written mostly in C++ (The same code is also used in other, non-iOS projects), but they're not found. Example: there is a file…
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
8
votes
2 answers

Exception not caught opening a non-existing file using C++

I was running a MWE from here: http://www.cplusplus.com/reference/ios/ios/exceptions/ On my machine it does not catch the exception. Here is my code #include #include int main() { std::ifstream file; file.exceptions(…
NOhs
  • 2,780
  • 3
  • 25
  • 59