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
255
votes
4 answers

Do I need to manually close an ifstream?

Do I need to manually call close() when I use a std::ifstream? For example, in the code: std::string readContentsOfFile(std::string fileName) { std::ifstream file(fileName.c_str()); if (file.good()) { std::stringstream buffer; …
Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42
98
votes
7 answers

Getting std :: ifstream to handle LF, CR, and CRLF?

Specifically I'm interested in istream& getline ( istream& is, string& str );. Is there an option to the ifstream constructor to tell it to convert all newline encodings to '\n' under the hood? I want to be able to call getline and have it…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
85
votes
6 answers

Fast textfile reading in c++

I am currently writing a program in c++ which includes reading lots of large text files. Each has ~400.000 lines with in extreme cases 4000 or more characters per line. Just for testing, I read one of the files using ifstream and the implementation…
Arne
  • 17,706
  • 5
  • 83
  • 99
77
votes
1 answer

C++ ifstream failbit and badbit

In case of ifstream in C++, under what conditions are failbit and badbit flags set ?
Jake
  • 16,329
  • 50
  • 126
  • 202
72
votes
3 answers

C++ ifstream error using string as opening file path.

I have: string filename: ifstream file(filename); The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something? Here's the error: error: no matching function for call to…
Mark
  • 8,408
  • 15
  • 57
  • 81
68
votes
1 answer

reading a line from ifstream into a string variable

In the following code : #include #include #include using namespace std; int main() { string x = "This is C++."; ofstream of("d:/tester.txt"); of << x; of.close(); ifstream read("d:/tester.txt"); …
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
55
votes
1 answer

Why am I getting this ifstream error?

Implicit instantiation of undefined template 'std::basic_ifstream>' #ifndef MAPPER_H #define MAPPER_H #include #include #include #include "KeyValue.h" #include "Parser.h" using namespace…
OghmaOsiris
  • 843
  • 2
  • 8
  • 14
51
votes
4 answers

How does ifstream's eof() work?

#include #include int main() { std::fstream inf( "ex.txt", std::ios::in ); while( !inf.eof() ) { std::cout << inf.get() << "\n"; } inf.close(); inf.clear(); inf.open( "ex.txt", std::ios::in ); …
roxrook
  • 13,511
  • 40
  • 107
  • 156
46
votes
2 answers

How to initialize std::unique_ptr in constructor?

A.hpp: class A { private: std::unique_ptr file; public: A(std::string filename); }; A.cpp: A::A(std::string filename) { this->file(new std::ifstream(filename.c_str())); } The error that I get is thrown: A.cpp:7:43:…
user1529891
43
votes
8 answers

How to count lines of a file in C++?

How can I count lines using the standard classes, fstream and ifstream?
malhobayyeb
  • 2,725
  • 11
  • 57
  • 91
38
votes
4 answers

tellg() function give wrong size of file?

I did a sample project to read a file into a buffer. When I use the tellg() function it gives me a larger value than the read function is actually read from the file. I think that there is a bug. here is my code: EDIT: void read_file (const char*…
Elior
  • 3,178
  • 6
  • 37
  • 67
33
votes
1 answer

Why can't I return std::getline's as-if-boolean result?

A standard idiom is while(std::getline(ifstream, str)) ... So if that works, why can't I say bool getval(std::string &val) { ... std::ifstream infile(filename); ... return std::getline(infile, val); } g++ says "cannot…
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
32
votes
4 answers

Checking if a file opened successfully with ifstream

I have the following that will open a file for reading. However, I want to check to make sure that the file was open successfully, so I am using the fail to see if the flags have been set. However, I keep getting the following error: I am new to…
ant2009
  • 27,094
  • 154
  • 411
  • 609
32
votes
2 answers

ifstream::is_open vs ifstream::fail?

Reading Savitch's Problem Solving in C++, std::ifstream::fail is shown as an example to check if a file has been correctly opened (ifstream or ofstream). I've previously used, as it is what I was first shown, std::ifstream::is_open to perform the…
OJFord
  • 10,522
  • 8
  • 64
  • 98
31
votes
3 answers

Creating fstream object from a FILE* pointer

The well known way of creating an fstream object is: ifstream fobj("myfile.txt"); ie. using a filename. But I want to create an ifstream object using a file descriptor. Reason: I want to execute a command using _popen(). _popen() returns the…
Tabrez Ahmed
  • 2,830
  • 6
  • 31
  • 48
1
2 3
99 100