Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

8.6 Reading Lines of Text

stdio.h

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.

Prototype
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:

  1. const int NAME_SIZE = 20;
  2. char name[NAME_SIZE];
  3.  
  4. printf("Enter student name: ");
  5. 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.

Tip
Tip

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:

  1. 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:

  1. int endpos = strlen(name);
  2. name[endpos-1] = '\0';

after which the array will now contain

Listing
  1. /* doublespace.cc
  2.  *
  3.  * Reads a text file and produces a new version in which the lines of text
  4.  * are double spaced.
  5.  *
  6.  * Prompts the user for both the source and destination file names.
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. const int FILENAME_SIZE = 50;
  12. const int MAXLINE_SIZE = 128;
  13.  
  14. void processFile(FILE *infile, FILE *outfile);
  15.  
  16.  
  17. int main()
  18. {
  19.     /* Prompt for and read the two file names. */  
  20.   char srcFileName[FILENAME_SIZE] = "";
  21.   char dstFileName[FILENAME_SIZE] = "";
  22.  
  23.   printf("Enter the source filename: ");
  24.   scanf("%s", srcFileName);
  25.  
  26.   printf("Enter the destination filename: ");
  27.   scanf("%s", dstFileName);
  28.  
  29.    /* Open and validate the two files. */    
  30.   FILE *infile;
  31.   FILE *outfile;
  32.  
  33.   infile = fopen(srcFileName, "r");
  34.   if(!infile) {
  35.     fprintf(stderr, "Error: source file can not be opened.\n");
  36.     return 1;
  37.   }
  38.  
  39.   outfile = fopen(dstFileName, "w");
  40.   if(!outfile) {
  41.     fprintf(stderr, "Error: destingation file can not be opened.\n");
  42.     return 1;
  43.   }
  44.  
  45.     /* Process the file. */
  46.   processFile(infile, outfile);
  47.  
  48.     /* Close the two files. */
  49.   fclose(infile);
  50.   fclose(outfile);
  51. }
  52.  
  53.  
  54.  /* Read the lines of text from the input file and write them to the
  55.     output file with blank lines between each line of text. */
  56. void processFile(FILE *infile, FILE *outfile)
  57. {
  58.   char line[MAXLINE_SIZE];
  59.  
  60.   fgets(line, MAXLINE_SIZE, infile);
  61.   while(!feof(infile)) {
  62.     fprintf(outfile, "%s\n", line);
  63.     fgets(line, MAXLINE_SIZE, infile);    
  64.   }
  65. }
Program: doublespace.cc
Question 8.6.1

Consider the following declarations

  1. char input[35];
  2. 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?

    1. fgets(input, 4, infile);
    1. fgets(input, 10, infile);
    1. fgets(input, 25, infile);
Question 8.6.2

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.

  1. int countLines(FILE *infile)
  2. {
  3.   char line[128];
  4.   int numLines = 0;
  5.  
  6.   fgets(line, 128, infile);
  7.   while(!feof(infile)) {  
  8.     numLines++;
  9.     fgets(line, 128, infile);
  10.   }
  11.  
  12.   return numLines;
  13. }