Function Read How Many Lines in a File C

Solarian Developer

My programming ramblings

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

Posted on April three, 2019 by Paul

In this commodity, I volition bear witness yous 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 role that can exist used with any standard C compiler.

Reading a file line by line is a trivial trouble in many programming languages, merely not in C. The standard way of reading a line of text in C is to use the fgets office, which is fine if you know in advance how long a line of text could be.

Yous can detect all the code examples and the input file at the GitHub repo for this commodity.

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

                                                                        one                                    #include                  <stdio.h>                                                        ii                                    #include                  <stdlib.h>                                                        3                                                        4                                    int                  main                  (                  void                  )                  {                                      5                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      seven                                    perror                  (                  "Unable to open file!"                  );                                      8                                    exit                  (                  one                  );                                      nine                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  Naught                  )                  {                  fourteen                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marker string used to show where the content of the chunk array has ended                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  nineteen                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      two                                    ~ $ ./t0                                      three                                    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|*                                      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 assortment, as filled after every call to fgets, and a marker string.

If you picket carefully, by scrolling the to a higher place text snippet to the correct, y'all can see that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking tin can store an entire line from the original text file only if the line can fit inside our chunk array.

What if y'all demand to have the unabridged 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 character.

Let's start by creating a line buffer that will shop the chunks of text, initially this volition take the aforementioned length equally the chunk array:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      8                                                        9                                    char                  clamper                  [                  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 retentivity for the line buffer."                  );                  sixteen                                    exit                  (                  1                  );                  17                                    }                  18                                    19                                    // "Empty" the string                  twenty                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

Side by side, we are going to append the content of the chunk array to the end of the line cord, until we notice the end of line character. If necessary, we'll resize the line buffer:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      half dozen                                    // ...                                      vii                                                        8                                    // "Empty" the string                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  x                                    11                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  12                                    // Resize the line buffer if necessary                  13                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  xiv                                    size_t                  chunk_used                  =                  strlen                  (                  clamper                  );                  fifteen                                    16                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  2                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  NULL                  )                  {                  19                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  20                                    gratuitous                  (                  line                  );                  21                                    exit                  (                  one                  );                  22                                    }                  23                                    }                  24                                    25                                    // Copy 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                                    // Bank check if line contains '\n', if yes procedure the line of text                  xxx                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  31                                    fputs                  (                  line                  ,                  stdout                  );                  32                                    fputs                  (                  "|*                  \due north                  "                  ,                  stdout                  );                  33                                    // "Empty" the line buffer                  34                                    line                  [                  0                  ]                  =                  '\0'                  ;                  35                                    }                  36                                    }                  37                                    38                                    fclose                  (                  fp                  );                  39                                    gratuitous                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \north                  "                  ,                  len                  );                  42                                    }                              

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

This is the result of running the above code on my motorcar. For brevity, I kept only the get-go lines of output:

                                                                        one                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      ii                                    ~ $ ./t1                                      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 condimentum.                                      6                                    |*                                      7                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.                                      eight                                    |*                                      9                                    Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh european union elementum.                  10                                    |*                              

You tin see that, this time, nosotros tin can print full lines of text and not fixed length chunks like in the initial approach.

Permit's modify the to a higher place code in gild to print the line length instead of the bodily text:

                                                                        1                                    // ...                                      2                                                        three                                    int                  primary                  (                  void                  )                  {                                      4                                    // ...                                      5                                                        6                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Goose egg                  )                  {                                      7                                                        8                                    // ...                                      9                                    10                                    // Check if line contains '\n', if yes process the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\n'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \northward                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  14                                    line                  [                  0                  ]                  =                  '\0'                  ;                  xv                                    }                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  nineteen                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \northward                  "                  ,                  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                                      ii                                    ~ $ ./t1                                      3                                    line length: 57                                      four                                    line length: 136                                      v                                    line length: 147                                      6                                    line length: 114                                      seven                                    line length: 112                                      8                                    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: ane                  15                                    line length: 834                  16                                    line length: i                  17                                    line length: 821                  eighteen                                    nineteen                                    20                                    Max line size: 1024                              

In the next case, I will show you lot how to use the getline office bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, and then y'all won't be able to easily test this example on a Windows system. However, you should be able to test information technology if you are using Cygwin or Windows Subsystem for Linux.

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        three                                    #include                  <string.h>                                                        4                                                        v                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      seven                                    if                  (                  fp                  ==                  NULL                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      ix                                    exit                  (                  1                  );                  10                                    }                  eleven                                    12                                    // Read lines using POSIX office getline                  13                                    // This code won't work on Windows                  xiv                                    char                  *                  line                  =                  NULL                  ;                  fifteen                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  18                                    printf                  (                  "line length: %zd                  \north                  "                  ,                  strlen                  (                  line                  ));                  19                                    }                  twenty                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    complimentary                  (                  line                  );                  // getline will resize the input buffer equally necessary                  25                                    // the user needs to free the memory when not needed!                  26                                    }                              

Please note, how simple is to employ POSIX'southward getline versus manually buffering chunks of line like in my previous example. It is unfortunate that the standard C library doesn't include an equivalent function.

When you use getline, don't forget to free 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 y'all need to keep it for further processing.

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

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      ii                                    ~ $ ./t2                                      3                                    line length: 57                                      four                                    line length: 136                                      5                                    line length: 147                                      half-dozen                                    line length: 114                                      vii                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  ten                                    line length: one                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: 1                  xv                                    line length: 834                  16                                    line length: one                  17                                    line length: 821                  eighteen                                    19                                    20                                    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 aforementioned lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on different Unix like systems.

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

We are going to have the above example and replace the POSIX's getline version with our ain implementation, say my_getline. Obviously, if you lot are on a POSIX system, y'all should utilise the version provided by the operating system, which was tested by countless users and tuned for optimal performance.

The POSIX getline part has this signature:

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

Since ssize_t is also a POSIX defined type, usually 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 nosotros are going to implement the role using the same approach as in ane 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:

                                                                        one                                    // This will but have effect on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      3                                    #ascertain _CRT_SECURE_NO_WARNINGS one                                      four                                    #define restrict __restrict                                      five                                    

0 Response to "Function Read How Many Lines in a File C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel