C++20 sections · 1024 units
Open in Course

What is ifstream?

Reading from files

ifstream stands for input file stream, your tool for reading data from files. Create one with ifstream file("input.txt");, and it opens input.txt for reading. If the file doesn't exist, the stream enters a fail state.

You read from ifstream using >> just like cin. The stream tracks your position in the file, moving forward with each read. When you reach the end, eof() returns true. Think of ifstream as a cursor scanning through a file.

You can read integers, strings, or lines, the same extraction operators work. The file stays on disk; you're just copying data into your program.