14.6. Line I/O

Time: 00:03:04 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides: PDF, PPTX
Review

Line-oriented text is a common data organization, with a book as a familiar metaphor. The lines may have fixed, predetermined lengths or various, independent lengths like a book. Lines are a property of textual data, implying that line-oriented I/O is appropriate for text but not binary files. The textbook first introduced the getline function in Chapter 8 to read strings from the console. 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 is to read the lines in a file one at a time into a string variable.

C++ provides a few versions of the get and getline functions to read lines but relies on the inserter operator, <<, to write them. C-string input: failed version and string object I/O example demonstrate why C++ programs typically don't use the extractor operator, >>, to read strings. Different versions of getline can save data to C-strings or instances of the string class, but get only supports C-strings. Therefore, programmers can choose which kind of string best matches a given program.

C++ programs typically only perform line-oriented I/O on files opened in text mode. Text mode is automatic on POSIX systems and the default on Windows. File streams opened in text mode automatically map the Window's /r/n line terminator to the POSIX \n, allowing the getline functions to identify the end of a line.

Line Input Functions

C++ provides several versions of the getline function, supplying the same features to C-strings and instances of the string class. They read a sequence of characters from a file and store them in a string. The two-parameter versions stop reading when they read and discard a newline character: \n. The three-parameter versions allow programmers to specify a delimiter or marker character, passing it in as the third parameter. They stop reading input when they read and discard the delimiter. Programs can specify a different delimiter with each function call, allowing them to parse data into meaningful units. The functions return the input stream, allowing programs to test their state flags to drive control statements conveniently.

istream& getline(istream& in, string& s);
The function reads one line of text from file in, saving it in the string parameter s. It discards the newline character, \n.
istream& getline(istream& in, string& s, char delim);
The function reads a sequence of characters from file in, saving them in the string parameter s, until it reads and discards the delimiter character saved in delim.
string class line-input functions. The getline functions read characters from the input stream, in, saving them in string s. The functions stop reading when they detect and discard a designated sentinel, either the newline or a program-specified delimiter. The s-rolodex.cpp example, presented in a subsequent section, demonstrates both getline versions. Please see getline for more detail and the move versions added by the 2011 ANSI update.
istream& getline(char* s, int n);
The function reads one line of text from file in, saving it in the C-string parameter s. It discards the newline character, \n.
istream& getline(char* s, int n, char delim);
The function reads a sequence of characters from file in, saving them in the C-string parameter s, until it reads and discards the delimiter character saved in delim.
istream& get(char* s, int n);
Parallels the two-parameter getline function but leaves the newline character in the input stream.
istream& get(char* s, int n, char delim);
Parallels the three-parameter getline function but leaves the delimiter character in the input stream.
C-string line-input functions. The get and getline functions read characters from the input stream, in, saving them in the C-string s. The functions stop reading when they read n - 1 characters or when they detect a designated sentinel or marker character, either the newline or the character saved in delim. The two versions of get described here are in addition to the character versions described previously. The c⁠-⁠rolodex.cpp example, presented in a subsequent section, demonstrates both getline functions. Please see getline for more detail. See get for a complete list of the overloaded functions, including two streambuf versions not covered in the textbook.

Common Programming Patterns

string Objects
string line;
while (getline(input, line))
{
    // process line
    output << line << endl;
}
 
 
string line;
getline(input, line);
while (line.length() > 0)
{
    // process line
    cout << line << endl;
    getline(input, line);
}
string line;
getline(input, line);
while (!input.eof())
{
    // process line
    cout << line << endl;
    getline(input, line);
}
C-Strings
char line[100];
while (input.getline(line, 100))
{
    // process line
    output << line << endl;
}
 
 
char line[100];
input.getline(line, 100);
while (strlen(line) > 0)
{
    // process line
    cout << line << endl;
    input.getline(line, 100);
}
char line[100];
input.getline(line, 100);
while (!input.eof())
{
    // process line
    cout << line << endl;
    input.getline(line, 100);
}
Line I/O examples. The examples demonstrate common line I/O patterns. They assume that input and output are instances of the ifstream and ofstream classes, opened in text mode. After processing the input, the characters saved in line may be unchanged from the input, modified, or replaced.