Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, November 17, 2024

File mode Detecting End-of-File

 

File Modes

Files can be opened in various modes using flags (e.g., ios::in, ios::out, etc.):

ModeDescription
ios::inOpen for input (reading).
ios::outOpen for output (writing).
ios::appAppend to the end of the file.
ios::truncTruncate (delete) the contents of the file.
ios::binaryOpen 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