14.8. Line I/O

Lines are a property of textual data, implying that line-oriented I/O is appropriate for text files but not for binary files. We first used the getline function, for reading strings from the console, in chapter 8. As cin is just a stream bound to the console, reading lines from a file is similar to reading strings from the console. Our approach here is to read the lines in a file one at a time into a string variable.

We also learned in chapter 8 that there are two versions of getline, one for reading C-strings and one for reading instances of the string class. This fact means that we can choose which kind of string variable we wish to use when performing line-oriented I/O. Furthermore, we'll find that the getline functions have a bit more to offer than just the basic features that we have used in the past.

Each getline function has two main, overloaded versions. In addition, there is a similar, overloaded version of get that behaves similar to the C-string versions of getline.

istream& getline(istream& in, string& s);
Reads one line of text from the input stream in and stores it in the string named s. It also removes the new line character from the input stream and discards it.
istream& getline(istream& in, string& s, char delim);
Reads text from the input stream in and stores it in string s. The function stops reading when the delimiter character, specified as the third argument, is encountered; the delimiter is removed from the input stream and discarded. See s-rolodex.cpp for an example of using delimiters with instances of the string class.
string class functions. Please see getline for more detail.
istream& getline(char* s, int n);
Reads a line of text from the input stream and stores it in the C-string s. It extracts the new line character from the input stream and discards it.
istream& getline(char* s, int n, char delim);
Reads text from the input stream until the delimiter character, delim, is encountered; the text is stored in the C-string s. The function extracts the delimiter character from the input stream and discards it. See c-rolodex.cpp for an example of using delimiters with the C-string version of the getline function
istream& get(char* s, int n);
Similar to getline 1 above but does not discard the new line character, which is retained at the end of the input string.
istream& get(char* s, int n, char delim);
Similar to getline 2 above but does not discard the delimiter character, which is retained at the end of the input string.
C-string functions. Please see getline and get for more detail.

Common Programming Patterns

ifstream input("input.txt");
ofstream output("output.txt);
(a)
string Objects
string line;
while (getline(input, line))
	output << line << endl;
string line;
while (!input.eof())
{
	getline(input, line);
	cout << line << endl;
}
(b)(c)
C-Strings
char line[100];
while (input.getline(line, 100))
	output << line << endl;
char line[100];
while (!input.eof())
{
	input.getline(line, 100);
	cout << line << endl;
}
(d)(e)
Line I/O examples. The simple code fragments illustrate the I/O function syntax and suggest different ways of reading or writing a file one line at a time. Notice that there are no specific output functions for lines; use operator << instead. Two of the patterns embed the read operation in the while-loop control, which provides a compact and efficient mechanism that reads all data in a file. The getline function reads all the text from the current file position up to and including the next new-line (i.e., line feed) character, and then discards the new-line character.
  1. Defining the input and output stream objects. Replace the file names with files appropriate to a given problem or with variables.
  2. getline reads one line of text from the input stream, discards the new-line character, stores the text in the second parameter, and returns input (the first parameter). The overloaded operator() forms an expression converting the returned input into the Boolean value needed to drive the loop.
  3. This pattern uses the eof function to drive the while-loop. Each line is read and processed inside the loop.
  4. Similar to pattern (a) but based on a C-string.
  5. Similar to pattern (b) but based on a C-string.