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
29
votes
1 answer

Rewind an ifstream object after hitting the end of file

Having a text file with a few characters (lets say 10), you can try to read 1000 characters from it. char *buf = new char[1000]; ifstream in("in.txt"); in.read(buf, 1000); This, of course, will set the eofbit flag (and the failbit too), however,…
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
28
votes
2 answers

Reading a text file - fopen vs. ifstream

Googling file input I found two ways to input text from a file - fopen and ifstream. Below are the two snippets. I have a text file consisting of one line with an integer I need to read in. Should I use fopen or ifstream? SNIPPET 1 - FOPEN FILE *…
user656925
28
votes
3 answers

What does ifstream::rdbuf() actually do?

I have the following code and it works pretty good (other than the fact that it's pretty slow, but I don't care much about that). It doesn't seem intuitive that this would write the entire contents of the infile to the outfile. // Returns 1 if…
Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96
28
votes
1 answer

C++ ifstream Error Checking

I am new to C++ and want to add error checking to my code plus I want to make sure I'm using good coding practices. I read a line from an ASCII file into a string using: ifstream paramFile; string tmp; //open input file tmp.clear(); paramFile >>…
slowmotionfred
  • 293
  • 1
  • 3
  • 8
25
votes
2 answers

What's wrong with the ifstream seekg

I am trying to do a seek and re-read the data. but the code fails. The code is std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary); std::streampos pos = ifs.tellg(); std::cout <<" Current pos: " << pos <<…
veda
  • 6,416
  • 15
  • 58
  • 78
24
votes
4 answers

C++ reading unsigned char from file stream

I want to read unsigned bytes from a binary file. So I wrote the following code. #include #include #include #include std::string filename("file"); size_t bytesAvailable = 128; size_t toRead =…
David
  • 562
  • 1
  • 3
  • 10
23
votes
5 answers

istream::getline return type

What does the istream::getline method return? I am asking because I have seen that to loop through a file, it should be done like this: while ( file.getline( char*, int ) ) { // handle input } What is being returned?
user542687
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
4 answers

How to read huge file in c++

If I have a huge file (eg. 1TB, or any size that does not fit into RAM. The file is stored on the disk). It is delimited by space. And my RAM is only 8GB. Can I read that file in ifstream? If not, how to read a block of file (eg. 4GB)?
ZigZagZebra
  • 1,349
  • 3
  • 14
  • 25
20
votes
5 answers

Open file by its full path in C++

I want the user to give me the full path where the file exists and not just the file name. How do I open the file this way? Is it something like this: ifstream file; file.open("C:/Demo.txt", ios::in); This doesn't seem to work.
Greg
19
votes
4 answers

Using C++ ifstream extraction operator>> to read formatted data from a file

As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents: just dummy Hello ofstream 555 My questions is how to read char data present in the…
goldenmean
  • 18,376
  • 54
  • 154
  • 211
19
votes
1 answer

C++, function that can take either ifstream or istringstream

I have a function do_something that reads unsigned characters from a stream. The stream can be created from a file given the file name. Or it can be created from the given string by considering it as data. I would like to reuse the function in both…
klk206
  • 454
  • 4
  • 8
18
votes
2 answers

C++: assign cin to an ifstream variable?

You know the common stdio idiom that stdin is specified by a filename of "-", e.g. if ((strcmp(fname, "-")) fp = fopen(fname); else fp = stdin; What's the best way to do this with an ifstream instance? I've received a bit of code that has…
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
18
votes
2 answers

How can I use non-default delimiters when reading a text file with std::fstream?

In my C++ code, I want to read from a text file (*.txt) and tokenize every entry. More specifically, I want to be able to read individual words from a file, such as "format", "stack", "Jason", "europe", etc. I chose to use fstream to perform this…
FrozenLand
  • 259
  • 1
  • 4
  • 14
16
votes
2 answers

use wstring get line read file

I have a file that contains text, I would like to get each line from this file into a std::wstring variable.If I do this I get an error, so is it possible to use std::wstring or I'm obligate to use a std::string ? This is my code : std::ifstream…
simon
  • 1,180
  • 3
  • 12
  • 33