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

How can I manually read PNG files in C++?

Portable Network Graphics Overview The general layout of any given PNG file looks like this: File Header: An 8-byte signature. Chunks: Chunks of data ranging from image properties to the actual image itself. The Problem I want to read PNG files in…
user3745189
  • 521
  • 1
  • 7
  • 17
16
votes
2 answers

ifstream and ofstream or fstream using in and out

When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream…
Ishaan
  • 707
  • 1
  • 7
  • 16
16
votes
1 answer

How to check whether ifstream is end of file in C++

I need to read all blocks of one large file(about 10GB) sequentially, the file contains many floats with a few strings, like this(each item splited by '\n'): 6.292611 -1.078219E-266 -2.305673E+065 sod;eiwo 4.899747e-237 1.673940e+089 -4.515213 I…
user1024
  • 982
  • 4
  • 13
  • 26
16
votes
1 answer

Why can't I initialize a reference to `ofstream` / `ifstream`, with an instance of `fstream`?

INTRODUCTION void read_foo (std::ifstream& out); void write_foo (std::ofstream& out); I have these two functions where one is supposed to read from a file, and the other is supposed to write to one. Everything works having the below…
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
15
votes
6 answers

Is std::ifstream significantly slower than FILE?

I've been informed that my library is slower than it should be, on the order of 30+ times too slow parsing a particular file (text file, size 326 kb). The user suggested that it may be that I'm using std::ifstream (presumably instead of FILE). I'd…
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
15
votes
4 answers

How to read formatted data in C++?

I have formatted data like the following: Words 5 AnotherWord 4 SomeWord 6 It's in a text file and I'm using ifstream to read it, but how do I separate the number and the word? The word will only consist of alphabets and there will…
TheOnly92
  • 1,723
  • 2
  • 17
  • 25
15
votes
1 answer

ifstream::read doesn't tell how many bytes it really reads?

I'm using ifstream::read to read a file, ifstream ifs("a.txt"); char buf[1024]; ifs.read(buf, 1024); But a.txt's size might be less than 1000 bytes, so how am I supposed to know how many bytes have been read from ifs?
Alcott
  • 17,905
  • 32
  • 116
  • 173
14
votes
6 answers

Check if a fstream is either a file or directory

I'm using C++ fstream to read a config file. #include std::ifstream my_file(my_filename); Right now, if I pass the path of a directory, it silently ignores this. E.g. my_file.good() returns true, even if my_filename is a directory. Since…
MacFreek
  • 3,207
  • 2
  • 31
  • 41
14
votes
4 answers

Returning ifstream in a function

Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
13
votes
6 answers

How to assign istringstream and ifstream to an istream variable?

I want to have a variable of type istream which can hold either the contents of a file or a string. The idea is that if no file was specified, the variable of type istream would be assigned with a string. std::ifstream…
oddRaven
  • 672
  • 1
  • 7
  • 20
13
votes
6 answers

C++ - Getting size in bits of integer

I need to know whether an integer is 32 bits long or not (I want to know if it's exactly 32 bits long (8 hexadecimal characters). How could I achieve this in C++? Should I do this with the hexadecimal representation or with the unsigned int one? My…
fergaral
  • 2,077
  • 6
  • 17
  • 34
13
votes
3 answers

C++ ifstream not reading \n?

I'm trying to do operations per-line of a text file, and the way I have it right now, my ifstream object isn't detecting the \n character for each line, which is required for this project. Here's how I have it right now: std::ifstream…
PulsePanda
  • 1,806
  • 10
  • 33
  • 56
13
votes
4 answers

Where does Visual Studio search for txt files when conducting file management operations?

I know this is a noob question, but I've worked with Python before and when you wanted to simply access a .txt file for example, all you had to do was make sure the txt file was in the same directory. I have the following C++ code below but it's not…
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79
12
votes
3 answers

Replace a line in text file

I want to replace a line of text in a file, but I don't know a functions to do this. I have this: ofstream outfile("text.txt"); ifstream infile("text.txt"); infile >> replace with other text; Any answers for this? I miss to say, for add text in…
Warkanlock
  • 156
  • 1
  • 1
  • 11
12
votes
9 answers

How can I speed up line by line reading of an ASCII file? (C++)

Here's a bit of code that is a considerable bottleneck after doing some measuring: //----------------------------------------------------------------------------- // Construct dictionary hash set from dictionary…
Jon
  • 1,379
  • 1
  • 12
  • 32