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

Reading from text file or stdin

I have a program that basically reads a text file and counts the number of occurrences of each word on each line. Everything works properly when reading from a text file using an ifstream, however, if a file name is not entered on the command line,…
CGutz
  • 537
  • 2
  • 7
  • 20
8
votes
1 answer

Read file by bits c++

i have binary file as input and i need to read it simple by bits. If i wanted to read file by characters i would use this: ifstream f(inFile, ios::binary | ios::in); char c; while (f.get(c)) { cout << c; } Output of this…
Pastx
  • 687
  • 3
  • 8
  • 23
8
votes
2 answers

Why can 'dd' read from a pipe faster than my own program using ifstream?

I have two programs that pass data to each other via linux pipes (named or otherwise). I need to hit a transfer rate of ~2600 MB/s between the two programs, but am currently seeing a slower rate of about ~2200 MB/s. However, I found that if I…
KyleL
  • 1,379
  • 2
  • 13
  • 35
8
votes
3 answers

Read text file into string. C++ ifstream

void docDB(){ int sdb = 0; ifstream dacb("kitudacbiet.txt"); if(!dacb.is_open()) cout<<"Deo doc dc file"<>dbiet[sdb].kitu; …
bulubuloa
  • 597
  • 1
  • 10
  • 21
7
votes
1 answer

Ifstream - Reset EOF bit

I'm working with ifstream. I read until EOF bit is set (I need this way). Why then don't work this: // IN is ifstream file. CH is char. if (IN.eof()) { IN.seekg(ios::beg); IN.clear(); if (read((char*)&CH, sizeof(CH))) cout <<…
Nanik
  • 603
  • 1
  • 12
  • 19
7
votes
2 answers

C++ Read File Until Space

Is there a way I can read data from a file until a space? I have a file John J. Doe and I want to read the file and put John in 1 variable, J. in another variable and Doe in a final variable. How do I do this with ifstream?
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
7
votes
2 answers

How to read unsigned int variables from file correctly, using ifstream?

My code reads unsigned int variables from the text file Input_File_Name. unsigned int Column_Count; //Cols unsigned int Row_Count;//Rows try { ifstream input_stream; input_stream.open(Input_File_Name,ios_base::in); if (input_stream) { …
Lucky Man
  • 1,488
  • 3
  • 19
  • 41
7
votes
6 answers

ifstream::open not working in Visual Studio debug mode

I've been all over the ifstream questions here on SO and I'm still having trouble reading a simple text file. I'm working with Visual Studio 2008. Here's my code: // CPPFileIO.cpp : Defines the entry point for the console application. // #include…
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
7
votes
3 answers

ifstream's operator>> to detect end of line?

I have an irregular list where the data look like this: [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [Number] [...] Notice that some lines have 2 numbers, some have 3…
Bonk
  • 1,859
  • 9
  • 28
  • 46
7
votes
6 answers

what exactly is a token, in relation to parsing

I have to use a parser and writer in c++, i am trying to implement the functions, however i do not understand what a token is. one of my function/operations is to check to see if there are more tokens to produce bool Parser::hasMoreTokens() how…
Technupe
  • 4,831
  • 14
  • 34
  • 37
7
votes
1 answer

Typo: bool to int conversion induced error in std::ifstream under Linux

I had a typo (|| instead of |) and noticed such a code fails with GCC and compiles with Visual. I know that the second parameter of std::ifstream is an int. So theoretically, a bool has to be converted implicitely to an int. So why it…
dgrat
  • 2,214
  • 4
  • 24
  • 46
7
votes
3 answers

Find the end of stream for cin & ifstream?

I'm running myself through a C++ text book that I have as a refresher to C++ programming. One of the practice problems (without going into too much detail) wants me to define a function that can be passed ifstream or cin (e.g. istream) as an…
user435219
  • 73
  • 1
  • 1
  • 4
7
votes
2 answers

How to read entire stream into a std::vector?

I read an answer here showing how to read an entire stream into a std::string with the following one (two) liner: std::istreambuf_iterator eos; std::string s(std::istreambuf_iterator(stream), eos); For doing something similar to…
Robinson
  • 9,666
  • 16
  • 71
  • 115
7
votes
3 answers

ifstream:: What is the maximum file size that a ifstream can read

I tried to read a 3GB data file using ifstream and it gives me wrong file size, whereas when I read a 600MB file, it gives me the correct result. In addition to wrong file size, I am also unable to read the entire file using ifstream. Here is the…
veda
  • 6,416
  • 15
  • 58
  • 78
7
votes
1 answer

Difference between casting ifstream to bool and using ifstream::is_open()

Maybe a dummy question, but I need a clear answer to it. Is there any difference at all in the return of any of those functions int FileExists(const std::string& filename) { ifstream file(filename.c_str()); return !!file; } int FileExists(const…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189