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.
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);
istream& getline(istream& in, string& s, char delim);
istream& getline(char* s, int n);
istream& getline(char* s, int n, char delim);
istream& get(char* s, int n);
istream& get(char* s, int n, char delim);
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); } |