0% found this document useful (0 votes)
23 views

Lecture 4 Notes: Reading From A File: //creating Object For Reading The Contents of File

1. To read from a text file in C++, use an ifstream object and open the file, checking for errors. 2. Read from the ifstream object like reading from cin, using the extraction (>>) operator. 3. Close the ifstream when done to release resources and make the file available to other programs.

Uploaded by

Mukul Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 4 Notes: Reading From A File: //creating Object For Reading The Contents of File

1. To read from a text file in C++, use an ifstream object and open the file, checking for errors. 2. Read from the ifstream object like reading from cin, using the extraction (>>) operator. 3. Close the ifstream when done to release resources and make the file available to other programs.

Uploaded by

Mukul Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LECTURE 4 NOTES: READING FROM A FILE

Reading a text file is very easy using an ifstream (input file stream).

1. Include the necessary headers.

#include <fstream>

using namespace std;

2. Declare an input file stream (ifstream) variable. For example,

ifstream inFile;

3. Open the file stream. Path names in MS Windows use backslashes (\). Because the backslash
is also the string escape character, it must be doubled. If the full path is not given, most
systems will look in the directory that contains the object program. For example,

inFile.open("C:\\temp\\datafile.txt");

4. Check that the file was opened. For example, the open fails if the file doesn't exist, or if it
can't be read because another program is writing it. A failure can be detected with code like
that below using the ! (logical not) operator:

if (!inFile) {

cerr << "Unable to open file datafile.txt";

exit(1); // call system to stop

5. Read from the stream in the same way as cin>>name (read from keyboard). For example,
inFile>>name; (read from file)
inFile>>marks;
6. Close the input stream. Closing is essential for output streams to be sure all information has
been written to the disk, but is also good practice for input streams to release system
resources and make the file available for other programs that might need to write it.

#include<fstream>

#include<iostream>

using namespace std;

int main()

ifstream f1; //creating object for reading the contents of file

char fname[10];

char ch;

cout<<"enter a file name..";

cin>>fname;
f1.open(fname); // opening the file using open( ) function and connecting it to the stream

if(f1.fail())

cerr<<"No such file exists..\n";

exit(1);

while(!f1.eof()) //Read until you reach end of file

ch=(char)f1.get(); //get function returns the end of file and reads char by char

cout.put(ch); // or you can also write cout<<ch;

f1.close();

return 0;

YOU CAN OPEN MULTIPLE FILES WITH SINGLE STREAM OBJECT BUT MAKE SURE TO CLOSE THE FIRST
FILE BEFORE OPENING THE SECOND FILE.

fail( ) stream function is used to check whether a file has been opened for input or output
successfully. If file opening operation fails, then fail( ) returns a non-zero character.

eof( ) stream function is used to check whether the file pointer has reached the end of a file
character or not. If successful, it returns a nonzero otherwise returns 1.

#include<fstream>

#include<iostream>

using namespace std;

int main()

{
ifstream f1; //creating object for reading the contents of file

char fname[10];

char ch;

f1.open("F:\\e drive\\Dev-Cpp\\2019-2020 C++\\try.cpp"); // opening the file using open( )


function and connecting it to the stream

if(f1.fail())

cerr<<"No such file exists..\n";

exit(1);

while(!f1.eof()) //Read until you reach end of file

ch=(char)f1.get(); //get function returns the end of file and reads char by char

cout.put(ch); // or you can also write cout<<ch;

f1.close();

return 0;

You might also like