14.4 TEXT FILES

Text files consist of characters. Text files are line oriented. In Windows Operating System, character newline (‘\n’) represents the end of line1. Text files can be opened either for input or for output.

If we want to open a text file for reading, we have to create an object of ifstream class. There are two methods to open a file for reading.

A) Use a suitable2 constructor of ifstream class.

ifstream f1(“salary.txt”);

B) Create a file stream object with default constructor and then call a method open() with it. For example:

ifstream f1;

f1.open(“f1in.txt”);

Here f1 is the object of the class ifstream. It is a user defined name. It calls the member function open() which takes the parameter as file name “f1in.txt” which is also ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.