File Modes
Files can be opened in various modes using flags (e.g., ios::in, ios::out, etc.):
| Mode | Description | 
|---|---|
ios::in | Open for input (reading). | 
ios::out | Open for output (writing). | 
ios::app | Append to the end of the file. | 
ios::trunc | Truncate (delete) the contents of the file. | 
ios::binary | Open in binary mode. | 
Example:
Detecting End-of-File (EOF)
The eof() function is used to check if the end of the file has been reached during a read operation.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
std::ifstream file("example.txt");
std::string line;
while (!file.eof()) {
std::getline(file, line);
std::cout << line << std::endl;
}
file.close();
return 0;
}
Output:

No comments:
Post a Comment