How to Use Fgets to Read Whole File

Solarian Developer

My programming ramblings

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

Posted on Apr 3, 2019 past Paul

In this article, I will prove you lot how to read a text file line by line in C using the standard C role fgets and the POSIX getline role. 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 past line is a picayune problem in many programming languages, but not in C. The standard way of reading a line of text in C is to use the fgets function, which is fine if you know in advance how long a line of text could be.

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

Allow'south start with a simple instance of using fgets to read chunks from a text file. :

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                                        4                                    int                  main                  (                  void                  )                  {                                      v                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      vii                                    perror                  (                  "Unable to open file!"                  );                                      8                                    exit                  (                  1                  );                                      9                                    }                  x                                    xi                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  14                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marking string used to show where the content of the chunk array has concluded                  16                                    }                  17                                    xviii                                    fclose                  (                  fp                  );                  xix                                    }                              

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

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

The lawmaking prints the content of the chunk array, every bit filled after every call to fgets, and a marker string.

If you watch carefully, by scrolling the higher up text snippet to the right, you tin see that the output was truncated to 127 characters per line of text. This was expected because our lawmaking can store an unabridged line from the original text file only if the line tin can fit within our chunk array.

What if yous demand to have the entire line of text available for further processing and not a slice of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until nosotros find the terminate of line character.

Allow's beginning by creating a line buffer that volition shop the chunks of text, initially this will take the same length as the chunk array:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        four                                                        5                                    int                  main                  (                  void                  )                  {                                      vi                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      viii                                                        nine                                    char                  chunk                  [                  128                  ];                  10                                    11                                    // Store the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  chunk                  );                  13                                    char                  *                  line                  =                  malloc                  (                  len                  );                  14                                    if                  (                  line                  ==                  NULL                  )                  {                  15                                    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 terminate of the line cord, until we observe the end of line character. If necessary, we'll resize the line buffer:

                                                                        ane                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        4                                                        5                                    int                  principal                  (                  void                  )                  {                                      6                                    // ...                                      vii                                                        8                                    // "Empty" the cord                                      ix                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    xi                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Cypher                  )                  {                  12                                    // Resize the line buffer if necessary                  13                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  chunk                  );                  15                                    xvi                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  2                  ;                  xviii                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  NULL                  )                  {                  19                                    perror                  (                  "Unable to reallocate retentiveness for the line buffer."                  );                  20                                    free                  (                  line                  );                  21                                    exit                  (                  1                  );                  22                                    }                  23                                    }                  24                                    25                                    // Re-create the chunk to the end of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  chunk                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Cheque if line contains '\northward', if yeah process the line of text                  30                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  31                                    fputs                  (                  line                  ,                  stdout                  );                  32                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  33                                    // "Empty" the line buffer                  34                                    line                  [                  0                  ]                  =                  '\0'                  ;                  35                                    }                  36                                    }                  37                                    38                                    fclose                  (                  fp                  );                  39                                    free                  (                  line                  );                  twoscore                                    41                                    printf                  (                  "                  \n\northward                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

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

This is the consequence of running the above 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                                      two                                    ~ $ ./t1                                      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 condimentum.                                      6                                    |*                                      seven                                    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.                  ten                                    |*                              

Y'all can see that, this fourth dimension, nosotros can print full lines of text and not fixed length chunks similar in the initial approach.

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

                                                                        1                                    // ...                                      ii                                                        3                                    int                  master                  (                  void                  )                  {                                      4                                    // ...                                      five                                                        half dozen                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                                      7                                                        8                                    // ...                                      9                                    10                                    // Bank check if line contains '\north', if yes process the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  i                  ]                  ==                  '\northward'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  xiii                                    // "Empty" the line buffer                  xiv                                    line                  [                  0                  ]                  =                  '\0'                  ;                  xv                                    }                  16                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  xix                                    gratuitous                  (                  line                  );                  xx                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

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

                                                                        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                                      vi                                    line length: 114                                      7                                    line length: 112                                      eight                                    line length: 95                                      9                                    line length: 62                  10                                    line length: 1                  11                                    line length: 428                  12                                    line length: ane                  13                                    line length: 460                  14                                    line length: 1                  xv                                    line length: 834                  xvi                                    line length: 1                  17                                    line length: 821                  18                                    19                                    20                                    Max line size: 1024                              

In the next example, I will show you how to use the getline function bachelor 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 example on a Windows system. Withal, yous should be able to exam it if yous are using Cygwin or Windows Subsystem for Linux.

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        three                                    #include                  <string.h>                                                        iv                                                        5                                    int                  primary                  (                  void                  )                  {                                      vi                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  NULL                  )                  {                                      viii                                    perror                  (                  "Unable to open up file!"                  );                                      9                                    exit                  (                  1                  );                  ten                                    }                  xi                                    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                  )                  !=                  -                  one                  )                  {                  18                                    printf                  (                  "line length: %zd                  \north                  "                  ,                  strlen                  (                  line                  ));                  xix                                    }                  twenty                                    21                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline will 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 you employ getline, don't forget to gratis the line buffer when you don't need it anymore. Also, calling getline more than than once will overwrite the line buffer, make a copy of the line content if you need to go along it for further processing.

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

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      two                                    ~ $ ./t2                                      3                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      6                                    line length: 114                                      7                                    line length: 112                                      viii                                    line length: 95                                      ix                                    line length: 62                  x                                    line length: 1                  11                                    line length: 428                  12                                    line length: ane                  xiii                                    line length: 460                  fourteen                                    line length: 1                  fifteen                                    line length: 834                  16                                    line length: ane                  17                                    line length: 821                  18                                    19                                    xx                                    Max line size: 960                              

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

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

Nosotros are going to have the above example and replace the POSIX's getline version with our own implementation, say my_getline. Patently, if you are on a POSIX organization, you lot should utilise the version provided by the operating arrangement, which was tested by countless 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 besides a POSIX defined blazon, normally 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 function using the same approach every bit in i of the to a higher place examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we institute the end of line grapheme:

                                                                        1                                    // This will just have effect on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      three                                    #define _CRT_SECURE_NO_WARNINGS i                                      4                                    #define restrict __restrict                                      5                                    

0 Response to "How to Use Fgets to Read Whole File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel