C How to Read Lines Inside a File Using Fgets

Solarian Programmer

My programming ramblings

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

Posted on April 3, 2019 by Paul

In this commodity, I will evidence you how to read a text file line past line in C using the standard C function fgets and the POSIX getline function. At the stop of the commodity, I will write a portable implementation of the getline office that tin exist used with any standard C compiler.

Reading a file line past line is a little problem in many programming languages, merely not in C. The standard manner of reading a line of text in C is to use the fgets function, which is fine if you know in accelerate how long a line of text could be.

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

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

                                                                        i                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        three                                                        four                                    int                  chief                  (                  void                  )                  {                                      5                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  Zilch                  )                  {                                      vii                                    perror                  (                  "Unable to open file!"                  );                                      eight                                    exit                  (                  i                  );                                      ix                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  fourteen                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marker string used to show where the content of the chunk array has concluded                  16                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  19                                    }                              

For testing the code I've used a elementary dummy file, lorem.txt. This is a piece from the output of the higher up program on my machine:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      2                                    ~ $ ./t0                                      iii                                    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|*                                      6                                    imentum.                                      7                                    |*                                      8                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |*                                      9                                    dignissim molestie.                  10                                    |*                              

The code prints the content of the clamper array, as filled afterward every call to fgets, and a mark cord.

If y'all watch advisedly, by scrolling the above text snippet to the right, you tin run across that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can store an entire line from the original text file just if the line can fit within our chunk assortment.

What if y'all need to have the unabridged line of text available for farther processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a split line buffer until we detect the cease of line grapheme.

Let's get-go by creating a line buffer that volition store the chunks of text, initially this volition accept the aforementioned length as the chunk array:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        four                                                        5                                    int                  main                  (                  void                  )                  {                                      half dozen                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      8                                                        ix                                    char                  chunk                  [                  128                  ];                  10                                    eleven                                    // Shop the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  clamper                  );                  thirteen                                    char                  *                  line                  =                  malloc                  (                  len                  );                  xiv                                    if                  (                  line                  ==                  NULL                  )                  {                  fifteen                                    perror                  (                  "Unable to allocate memory for the line buffer."                  );                  16                                    exit                  (                  1                  );                  17                                    }                  eighteen                                    19                                    // "Empty" the string                  xx                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

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

                                                                        1                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    // ...                                      7                                                        eight                                    // "Empty" the string                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    11                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  Goose egg                  )                  {                  12                                    // Resize the line buffer if necessary                  thirteen                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  chunk                  );                  15                                    16                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  2                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  Nil                  )                  {                  19                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  twenty                                    complimentary                  (                  line                  );                  21                                    exit                  (                  1                  );                  22                                    }                  23                                    }                  24                                    25                                    // Copy the chunk to the cease of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  clamper                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Check if line contains '\north', if aye process the line of text                  thirty                                    if                  (                  line                  [                  len_used                  -                  i                  ]                  ==                  '\n'                  )                  {                  31                                    fputs                  (                  line                  ,                  stdout                  );                  32                                    fputs                  (                  "|*                  \north                  "                  ,                  stdout                  );                  33                                    // "Empty" the line buffer                  34                                    line                  [                  0                  ]                  =                  '\0'                  ;                  35                                    }                  36                                    }                  37                                    38                                    fclose                  (                  fp                  );                  39                                    free                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \northward                  "                  ,                  len                  );                  42                                    }                              

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

This is the effect of running the in a higher place code on my machine. For brevity, I kept only the first lines of output:

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

You tin can see that, this fourth dimension, we tin print full lines of text and not stock-still length chunks like in the initial approach.

Let's modify the above code in order to impress the line length instead of the actual text:

                                                                        ane                                    // ...                                      two                                                        3                                    int                  main                  (                  void                  )                  {                                      4                                    // ...                                      5                                                        6                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Zippo                  )                  {                                      seven                                                        viii                                    // ...                                      9                                    10                                    // Cheque if line contains '\n', if yes process the line of text                  xi                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \north                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  xiv                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  xix                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \due north\due north                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

This is the result of running the modified code on my machine:

                                                                        i                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      3                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      6                                    line length: 114                                      vii                                    line length: 112                                      8                                    line length: 95                                      nine                                    line length: 62                  10                                    line length: i                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: ane                  15                                    line length: 834                  xvi                                    line length: i                  17                                    line length: 821                  18                                    nineteen                                    xx                                    Max line size: 1024                              

In the side by side example, I will bear witness you how to use the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so you won't be able to easily test this instance on a Windows organization. However, you should be able to test it if you are using Cygwin or Windows Subsystem for Linux.

                                                                        ane                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        four                                                        5                                    int                  primary                  (                  void                  )                  {                                      six                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  Aught                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      nine                                    go out                  (                  1                  );                  10                                    }                  eleven                                    12                                    // Read lines using POSIX function getline                  13                                    // This code won't work on Windows                  14                                    char                  *                  line                  =                  NULL                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  xviii                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  xix                                    }                  20                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    gratuitous                  (                  line                  );                  // getline will resize the input buffer as necessary                  25                                    // the user needs to free the memory when not needed!                  26                                    }                              

Please annotation, how simple is to use POSIX'south getline versus manually buffering chunks of line similar in my previous example. Information technology is unfortunate that the standard C library doesn't include an equivalent office.

When you lot utilise getline, don't forget to free the line buffer when you don't need information technology anymore. Also, calling getline more than in one case will overwrite the line buffer, make a copy of the line content if yous need to keep it for further processing.

This is the result of running the above getline example on a Linux machine:

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      ii                                    ~ $ ./t2                                      3                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      6                                    line length: 114                                      7                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  10                                    line length: 1                  eleven                                    line length: 428                  12                                    line length: one                  xiii                                    line length: 460                  14                                    line length: one                  15                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  xviii                                    19                                    20                                    Max line size: 960                              

It is interesting to note, that for this particular example the getline part on Linux resizes the line buffer to a max of 960 bytes. If yous run the same code on macOS the line buffer is resized to 1024 bytes. This is due to the unlike ways in which getline is implemented on different Unix like systems.

Equally mentioned earlier, getline is not present in the C standard library. It could exist an interesting practise to implement a portable version of this part. The thought here is not to implement the most performant version of getline, but rather to implement a simple replacement for not POSIX systems.

We are going to take the above example and replace the POSIX's getline version with our ain implementation, say my_getline. Manifestly, if you are on a POSIX arrangement, you should employ the version provided by the operating organization, which was tested by endless users and tuned for optimal performance.

The POSIX getline function has this signature:

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

Since ssize_t is likewise a POSIX defined type, unremarkably a 64 bits 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 we are going to implement the part using the same approach equally in ane of the in a higher place examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line grapheme:

                                                                        1                                    // This volition but have effect on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      3                                    #define _CRT_SECURE_NO_WARNINGS 1                                      4                                    #define restrict __restrict                                      5                                    

Related Posts

0 Response to "C How to Read Lines Inside a File Using Fgets"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel