Read from File in C++
Reading Data from a File in C++.
BeginnerFile Handling ProgramsExample 2 of 6
read-from-file.cpp
1#include <iostream>2#include <fstream>3#include <string>4using namespace std;56int main() {7 // Open file for reading8 ifstream inFile("data.txt");910 if (inFile.is_open()) {11 string line;1213 cout << "Reading file line by line:" << endl;14 while (getline(inFile, line)) {15 cout << line << endl;16 }1718 inFile.close();19 } else {20 cout << "Error: Unable to open file for reading." << endl;21 }2223 // Alternative: Read word by word24 ifstream inFile2("data.txt");25 if (inFile2.is_open()) {26 string word;27 cout << "\nReading file word by word:" << endl;28 while (inFile2 >> word) {29 cout << word << " ";30 }31 cout << endl;32 inFile2.close();33 }3435 return 0;36}
Output
Reading file line by line: Hello, World! This is a C++ file handling program. Line 3: Learning file operations. 100 200 300 Reading file word by word: Hello, World! This is a C++ file handling program. Line 3: Learning file operations. 100 200 300
What's going on
The program demonstrates file reading operations:
Opening file for reading
Reading line by line using getline()
Reading word by word using >> operator
Checking if file opened successfully
Closing file after reading
File reading enables data loading and file processing.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <fstream>
Provides file stream classes (ofstream, ifstream).
3.#include <string>
Provides string class for string operations.
Understanding File Reading
ifstream Concept
:
Input file stream
Used for reading from files
Similar to cin but reads from file
File must exist to read
Reading Methods
:
getline(): reads entire line
>> operator: reads word by word
read(): reads binary data
Opening File for Reading
Creating ifstream
:
ifstream inFile("data.txt");
How it works
:
Creates ifstream object
Opens file "data.txt" for reading
File must exist
Returns error if file not found
Checking Success
:
if (inFile.is_open()) {
}
1// File opened successfully
Reading Line by Line
Using getline()
:
string line;
while (getline(inFile, line)) {
}
1cout << line << endl;
How it works
:
getline() reads until newline
Returns false at end of file
Preserves line structure
Reads entire line including spaces
Reading Word by Word
Using >> Operator
:
string word;
while (inFile >> word) {
}
1cout << word << " ";
How it works
:
>> operator reads until whitespace
Stops at space, tab, newline
Skips whitespace automatically
Returns false at end of file
When to Use Each Method
getline()
:
Preserve line structure
Read formatted lines
Process line by line
Keep spaces in line
>> Operator
:
Read individual words
Skip whitespace
Parse space-separated data
Process tokens
Important Considerations
File Existence
:
File must exist to read
Check is_open() before reading
Handle file not found errors
Provide user feedback
End of File
:
Loop until end of file
getline() and >> return false at EOF
File pointer moves automatically
Don't read past EOF
File Paths
:
Relative or absolute paths
Check file permissions
Handle access errors
return 0;
This ends the program successfully.