How to Read a File Line by Line C#

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 past Paul

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

Reading a file line by line is a trivial problem in many programming languages, merely non in C. The standard fashion of reading a line of text in C is to utilize the fgets function, which is fine if you lot know in advance how long a line of text could be.

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

Allow'southward beginning with a simple case of using fgets to read chunks from a text file. :

                                                                        one                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                                        4                                    int                  main                  (                  void                  )                  {                                      5                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      seven                                    perror                  (                  "Unable to open up file!"                  );                                      8                                    exit                  (                  1                  );                                      9                                    }                  10                                    xi                                    char                  clamper                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Zero                  )                  {                  14                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \due north                  "                  ,                  stdout                  );                  // marker cord used to show where the content of the chunk array has concluded                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  19                                    }                              

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

                                                                        i                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      two                                    ~ $ ./t0                                      three                                    Lorem ipsum dolor sit down amet, consectetur adipiscing elit.                                      4                                    |*                                      five                                    Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|*                                      vi                                    imentum.                                      seven                                    |*                                      8                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |*                                      ix                                    dignissim molestie.                  ten                                    |*                              

The lawmaking prints the content of the clamper array, as filled after every phone call to fgets, and a marker string.

If you lot scout carefully, by scrolling the above text snippet to the right, you can see 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 only if the line can fit within our chunk array.

What if yous need to accept 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 separate line buffer until we find the end of line grapheme.

Let's beginning by creating a line buffer that will store the chunks of text, initially this will accept the same length every bit the chunk array:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        5                                    int                  chief                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      vii                                    // ...                                      8                                                        nine                                    char                  chunk                  [                  128                  ];                  ten                                    11                                    // Store the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  clamper                  );                  13                                    char                  *                  line                  =                  malloc                  (                  len                  );                  xiv                                    if                  (                  line                  ==                  Aught                  )                  {                  xv                                    perror                  (                  "Unable to classify memory for the line buffer."                  );                  16                                    exit                  (                  1                  );                  17                                    }                  xviii                                    nineteen                                    // "Empty" the string                  20                                    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 string, until we find the cease of line character. If necessary, we'll resize the line buffer:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        three                                    #include                  <cord.h>                                                        4                                                        5                                    int                  chief                  (                  void                  )                  {                                      half-dozen                                    // ...                                      7                                                        8                                    // "Empty" the string                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    11                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Cipher                  )                  {                  12                                    // Resize the line buffer if necessary                  xiii                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  clamper                  );                  15                                    sixteen                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  ii                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  Zippo                  )                  {                  19                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  20                                    costless                  (                  line                  );                  21                                    exit                  (                  1                  );                  22                                    }                  23                                    }                  24                                    25                                    // Re-create the chunk to the end of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  clamper                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Check if line contains '\n', if aye procedure the line of text                  thirty                                    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                                    free                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \due north\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

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

This is the result of running the to a higher place code on my auto. For brevity, I kept just the showtime lines of output:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      3                                    Lorem ipsum dolor sit down amet, consectetur adipiscing elit.                                      iv                                    |*                                      5                                    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.                                      8                                    |*                                      9                                    Aliquam erat volutpat. Mauris dignissim augue air-conditioning purus placerat scelerisque. Donec eleifend ut nibh eu elementum.                  10                                    |*                              

Y'all can run across that, this time, we can print full lines of text and not fixed length chunks like in the initial approach.

Let'southward modify the above code in gild to print the line length instead of the actual text:

                                                                        1                                    // ...                                      two                                                        three                                    int                  principal                  (                  void                  )                  {                                      four                                    // ...                                      v                                                        6                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                                      vii                                                        8                                    // ...                                      9                                    10                                    // Check if line contains '\n', if yep procedure the line of text                  xi                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  fourteen                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  16                                    }                  17                                    xviii                                    fclose                  (                  fp                  );                  19                                    complimentary                  (                  line                  );                  twenty                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      iii                                    line length: 57                                      4                                    line length: 136                                      five                                    line length: 147                                      half-dozen                                    line length: 114                                      7                                    line length: 112                                      8                                    line length: 95                                      ix                                    line length: 62                  ten                                    line length: 1                  eleven                                    line length: 428                  12                                    line length: i                  13                                    line length: 460                  xiv                                    line length: 1                  15                                    line length: 834                  sixteen                                    line length: 1                  17                                    line length: 821                  xviii                                    xix                                    20                                    Max line size: 1024                              

In the next example, I volition bear witness you how to utilize the getline role 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 hands exam this case on a Windows system. Withal, you should be able to test it if you lot are using Cygwin or Windows Subsystem for Linux.

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <cord.h>                                                        iv                                                        5                                    int                  primary                  (                  void                  )                  {                                      half-dozen                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  Zilch                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      9                                    exit                  (                  1                  );                  10                                    }                  eleven                                    12                                    // Read lines using POSIX office getline                  13                                    // This code won't piece of work on Windows                  xiv                                    char                  *                  line                  =                  Zero                  ;                  fifteen                                    size_t                  len                  =                  0                  ;                  sixteen                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  18                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  xix                                    }                  20                                    21                                    printf                  (                  "                  \north\due north                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline volition resize the input buffer as necessary                  25                                    // the user needs to free the retentivity when not needed!                  26                                    }                              

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

When you apply getline, don't forget to free the line buffer when you don't demand it anymore. As well, calling getline more than than one time volition overwrite the line buffer, make a copy of the line content if y'all need to keep it for further processing.

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

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      2                                    ~ $ ./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                  x                                    line length: 1                  eleven                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: 1                  15                                    line length: 834                  16                                    line length: i                  17                                    line length: 821                  18                                    19                                    20                                    Max line size: 960                              

It is interesting to annotation, that for this detail instance the getline function on Linux resizes the line buffer to a max of 960 bytes. If you lot run the aforementioned code on macOS the line buffer is resized to 1024 bytes. This is due to the dissimilar ways in which getline is implemented on different Unix like systems.

As mentioned earlier, getline is not present in the C standard library. Information technology could be an interesting exercise to implement a portable version of this function. The thought here is not to implement the most performant version of getline, but rather to implement a simple replacement for non POSIX systems.

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

The POSIX getline role has this signature:

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

Since ssize_t is besides a POSIX divers type, unremarkably a 64 bits signed integer, this is how we are going to declare our version:

                                                    i                                    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 as in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line character:

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

0 Response to "How to Read a File Line by Line C#"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel