Using Fgets in C to Read a File

Solarian Programmer

My programming ramblings

C Programming - read a file line by line with fgets and getline, implement a portable getline version

Posted on Apr 3, 2019 by Paul

In this article, I will show you how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline function that can exist used with any standard C compiler.

Reading a file line by line is a piddling trouble in many programming languages, but not in C. The standard mode of reading a line of text in C is to use the fgets function, which is fine if y'all know in advance how long a line of text could be.

You tin can find all the lawmaking examples and the input file at the GitHub repo for this article.

Let'southward first with a simple instance of using fgets to read chunks from a text file. :

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                                        four                                    int                  principal                  (                  void                  )                  {                                      5                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      six                                    if                  (                  fp                  ==                  NULL                  )                  {                                      7                                    perror                  (                  "Unable to open file!"                  );                                      viii                                    go out                  (                  1                  );                                      ix                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Cipher                  )                  {                  xiv                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // mark string used to bear witness where the content of the chunk array has ended                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  19                                    }                              

For testing the code I've used a simple dummy file, lorem.txt. This is a piece from the output of the above plan on my auto:

                                                                        one                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      2                                    ~ $ ./t0                                      3                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.                                      4                                    |*                                      5                                    Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|*                                      half dozen                                    imentum.                                      7                                    |*                                      eight                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |*                                      9                                    dignissim molestie.                  10                                    |*                              

The lawmaking prints the content of the chunk assortment, as filled after every call to fgets, and a marking string.

If you lookout carefully, by scrolling the above text snippet to the correct, you tin can run into that the output was truncated to 127 characters per line of text. This was expected because our code can store an unabridged line from the original text file just if the line can fit within our chunk assortment.

What if you lot need to have the entire line of text available for further processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a carve up line buffer until we find the finish of line character.

Let's start by creating a line buffer that volition store the chunks of text, initially this will have the same length equally the chunk assortment:

                                                                        i                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        4                                                        five                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      vii                                    // ...                                      viii                                                        9                                    char                  clamper                  [                  128                  ];                  x                                    11                                    // Store the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  chunk                  );                  13                                    char                  *                  line                  =                  malloc                  (                  len                  );                  fourteen                                    if                  (                  line                  ==                  NULL                  )                  {                  15                                    perror                  (                  "Unable to allocate memory for the line buffer."                  );                  16                                    get out                  (                  one                  );                  17                                    }                  18                                    19                                    // "Empty" the string                  20                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

Next, we are going to append the content of the chunk array to the finish of the line string, until we find the end of line grapheme. If necessary, we'll resize the line buffer:

                                                                        1                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        4                                                        v                                    int                  chief                  (                  void                  )                  {                                      six                                    // ...                                      seven                                                        8                                    // "Empty" the string                                      nine                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    11                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Cipher                  )                  {                  12                                    // Resize the line buffer if necessary                  13                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  xiv                                    size_t                  chunk_used                  =                  strlen                  (                  chunk                  );                  fifteen                                    16                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  ii                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  NULL                  )                  {                  19                                    perror                  (                  "Unable to reallocate retentivity for the line buffer."                  );                  20                                    free                  (                  line                  );                  21                                    exit                  (                  ane                  );                  22                                    }                  23                                    }                  24                                    25                                    // Re-create the chunk to the cease of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  chunk                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Check if line contains '\n', if yes process the line of text                  30                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  31                                    fputs                  (                  line                  ,                  stdout                  );                  32                                    fputs                  (                  "|*                  \northward                  "                  ,                  stdout                  );                  33                                    // "Empty" the line buffer                  34                                    line                  [                  0                  ]                  =                  '\0'                  ;                  35                                    }                  36                                    }                  37                                    38                                    fclose                  (                  fp                  );                  39                                    gratuitous                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \n\north                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

Please note, that in the above code, every time the line buffer needs to be resized its capacity is doubled.

This is the result of running the above lawmaking on my machine. For brevity, I kept only the kickoff lines of output:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      iii                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.                                      4                                    |*                                      v                                    Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum.                                      6                                    |*                                      7                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.                                      viii                                    |*                                      9                                    Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh eu elementum.                  10                                    |*                              

Y'all tin can see that, this time, we can print total lines of text and not stock-still length chunks like in the initial approach.

Let'south alter the above code in order to impress the line length instead of the bodily text:

                                                                        ane                                    // ...                                      ii                                                        iii                                    int                  main                  (                  void                  )                  {                                      four                                    // ...                                      5                                                        6                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  NULL                  )                  {                                      vii                                                        viii                                    // ...                                      9                                    10                                    // Check if line contains '\n', if yes process the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  one                  ]                  ==                  '\n'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  fourteen                                    line                  [                  0                  ]                  =                  '\0'                  ;                  fifteen                                    }                  16                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  nineteen                                    gratis                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \due north\due north                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

This is the issue of running the modified code on my motorcar:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      3                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      six                                    line length: 114                                      7                                    line length: 112                                      eight                                    line length: 95                                      9                                    line length: 62                  10                                    line length: i                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: 1                  xv                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  18                                    19                                    twenty                                    Max line size: 1024                              

In the next example, I will prove yous how to employ the getline role available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent function, so you won't be able to hands test this example on a Windows system. However, you should exist able to test it if you are using Cygwin or Windows Subsystem for Linux.

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        iv                                                        five                                    int                  main                  (                  void                  )                  {                                      six                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  Null                  )                  {                                      8                                    perror                  (                  "Unable to open up file!"                  );                                      9                                    exit                  (                  1                  );                  10                                    }                  eleven                                    12                                    // Read lines using POSIX function getline                  thirteen                                    // This code won't work on Windows                  14                                    char                  *                  line                  =                  Zippo                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  18                                    printf                  (                  "line length: %zd                  \northward                  "                  ,                  strlen                  (                  line                  ));                  xix                                    }                  20                                    21                                    printf                  (                  "                  \n\due north                  Max line size: %zd                  \north                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline volition resize the input buffer as necessary                  25                                    // the user needs to free the memory when not needed!                  26                                    }                              

Please note, how simple is to use POSIX's getline versus manually buffering chunks of line like in my previous case. Information technology is unfortunate that the standard C library doesn't include an equivalent function.

When yous use getline, don't forget to costless the line buffer when y'all don't need it anymore. Too, calling getline more than than once volition overwrite the line buffer, brand a copy of the line content if y'all need to continue information technology for further processing.

This is the event of running the above getline example on a Linux motorcar:

                                                                        ane                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      2                                    ~ $ ./t2                                      iii                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      half dozen                                    line length: 114                                      7                                    line length: 112                                      8                                    line length: 95                                      ix                                    line length: 62                  10                                    line length: i                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  fourteen                                    line length: ane                  15                                    line length: 834                  16                                    line length: i                  17                                    line length: 821                  18                                    19                                    twenty                                    Max line size: 960                              

It is interesting to notation, that for this particular case the getline role on Linux resizes the line buffer to a max of 960 bytes. If you run the same lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on dissimilar Unix similar systems.

As mentioned earlier, getline is non present in the C standard library. It could be an interesting practise to implement a portable version of this function. The thought here is non to implement the near performant version of getline, but rather to implement a elementary replacement for non POSIX systems.

Nosotros are going to accept the to a higher place instance and replace the POSIX's getline version with our own implementation, say my_getline. Manifestly, if you are on a POSIX system, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal performance.

The POSIX getline office has this signature:

                                                    i                                    ssize_t                  getline                  (                  char                  **                  restrict                  lineptr                  ,                  size_t                  *                  restrict                  n                  ,                  FILE                  *                  restrict                  stream                  );                              

Since ssize_t is also a POSIX divers blazon, unremarkably a 64 $.25 signed integer, this is how we are going to declare our version:

                                                    1                                    int64_t                  my_getline                  (                  char                  **                  restrict                  line                  ,                  size_t                  *                  restrict                  len                  ,                  FILE                  *                  restrict                  fp                  );                              

In principle nosotros are going to implement the office using the same arroyo every bit in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros found the end of line grapheme:

                                                                        1                                    // This volition only have outcome on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      iii                                    #ascertain _CRT_SECURE_NO_WARNINGS i                                      4                                    #ascertain restrict __restrict                                      5                                    

0 Response to "Using Fgets in C to Read a File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel