8.6 Reading Lines of Text
To read strings that contain blank spaces or full lines of text, you must use the fgets function. This function is defined in the stdio.h module.
|
Reading Lines of Text
stdio.h
char[] fgets(char buffer[], int maxSize, FILE *stream);
Reads up to
maxSize - 1 characters from the input stream or until it encounters the newline character (\n) or the end of file. The characters are stored in the provided character array and appended with the null character. |
Only the fgets function is defined for reading full lines of text, there is no separate function for reading from standard input. The fgets function, however, can also be used to read from standard input. Consider the following code segment which can be used to read in a full name:
- const int NAME_SIZE = 20;
- char name[NAME_SIZE];
- printf("Enter student name: ");
- fgets(name, NAME_SIZE, stdin);
This function can only be used to read strings, thus no format specifier has to be specified. Instead, you must pass the name of a character array that will be used to store the string, along with the size of that array, and the input file stream pointer. The function reads characters from the input stream and stores them into the provided character array until one of following occurs:
- it has read up to one less than the indicated size as specified by the second argument;
- it reads the newline character (
\n), which will be included and stored as part of the string; - it encounters the end of file.
After the input text is read, the null character is appended to the end of the text in order to create a string. In our example above, if you again enter
John Smith
fgets will read the entire line of text and the array will contain
The doublespace.cc program provided below illustrates the use of the fgets function to read lines of text from an input file and writes the lines to an output file while adding a blank line between each line of text.

Note that the fgets function includes the newline character as part of the string. Typically when we read strings of text, we do not want the newline character to be included in the string. After reading the input string, you need to remove the newline character. This can be done by setting the array element that contains the newline character to the null character:
- name[endpos] = '\0';
But how do we know where the newline character is in the array? Since the fgets function stops reading when it encounters the newline character, it will always be at the end of the string. C does not provide a standard function for striping off whitespace at the end of string as Python does. Instead, we can use the strlen function to find the length of the string and use that value to locate the newline character:
- int endpos = strlen(name);
- name[endpos-1] = '\0';
after which the array will now contain
Program: doublespace.cc
|
Consider the following declarations
- char input[35];
- FILE *infile = fopen("mary.txt", "r");
and assume the file was opened successfully. If the input file contains the following lines of text:
Mary had a little lamb Whose fleece was white as snow. And everywhere that Mary went, The lamb was sure to go.
What would be the result of executing the following statements, assuming each input starts with the first character in the file?
-
- fgets(input, 4, infile);
-
- fgets(input, 10, infile);
-
- fgets(input, 25, infile);
Design and write a function countLines that takes an input file stream and counts and returns the number of lines in the text file. Assume no line in the file is longer than 126 characters in length.
- int countLines(FILE *infile)
- {
- char line[128];
- int numLines = 0;
- fgets(line, 128, infile);
- while(!feof(infile)) {
- numLines++;
- fgets(line, 128, infile);
- }
- return numLines;
- }

