C Read Character Length of Line From File

Solarian Programmer

My programming ramblings

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

Posted on April 3, 2019 past Paul

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

Reading a file line by line is a trivial problem in many programming languages, simply not in C. The standard style of reading a line of text in C is to use the fgets role, which is fine if you know in advance how long a line of text could exist.

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

Let's kickoff with a elementary case of using fgets to read chunks from a text file. :

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                                        4                                    int                  master                  (                  void                  )                  {                                      five                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      7                                    perror                  (                  "Unable to open file!"                  );                                      eight                                    exit                  (                  1                  );                                      ix                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  14                                    fputs                  (                  clamper                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \north                  "                  ,                  stdout                  );                  // marker cord used to bear witness where the content of the chunk array has ended                  16                                    }                  17                                    xviii                                    fclose                  (                  fp                  );                  nineteen                                    }                              

For testing the code I've used a simple dummy file, lorem.txt. This is a piece from the output of the above programme 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.                  ten                                    |*                              

The code prints the content of the chunk array, equally filled after every call to fgets, and a mark string.

If you lookout man carefully, past 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 because our code tin can store an entire line from the original text file simply if the line can fit within our chunk assortment.

What if you need 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 carve up line buffer until nosotros find the end of line character.

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

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      viii                                                        9                                    char                  clamper                  [                  128                  ];                  x                                    xi                                    // Shop the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  chunk                  );                  thirteen                                    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                  20                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

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

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        iv                                                        v                                    int                  main                  (                  void                  )                  {                                      6                                    // ...                                      7                                                        eight                                    // "Empty" the string                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  ten                                    11                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  12                                    // Resize the line buffer if necessary                  xiii                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  clamper                  );                  15                                    xvi                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  two                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  Null                  )                  {                  nineteen                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  20                                    costless                  (                  line                  );                  21                                    exit                  (                  1                  );                  22                                    }                  23                                    }                  24                                    25                                    // Copy the clamper to the terminate of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  chunk                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Cheque if line contains '\n', if yes procedure the line of text                  30                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\north'                  )                  {                  31                                    fputs                  (                  line                  ,                  stdout                  );                  32                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  33                                    // "Empty" the line buffer                  34                                    line                  [                  0                  ]                  =                  '\0'                  ;                  35                                    }                  36                                    }                  37                                    38                                    fclose                  (                  fp                  );                  39                                    complimentary                  (                  line                  );                  twoscore                                    41                                    printf                  (                  "                  \due north\north                  Max line size: %zd                  \due north                  "                  ,                  len                  );                  42                                    }                              

Please annotation, that in the above code, every time 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 beginning lines of output:

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

You lot can run across that, this time, we can print full lines of text and not fixed length chunks similar in the initial approach.

Allow's change the to a higher place code in order to print the line length instead of the bodily text:

                                                                        1                                    // ...                                      ii                                                        three                                    int                  chief                  (                  void                  )                  {                                      iv                                    // ...                                      5                                                        6                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  Nil                  )                  {                                      7                                                        8                                    // ...                                      nine                                    10                                    // Check if line contains '\n', if yep procedure the line of text                  eleven                                    if                  (                  line                  [                  len_used                  -                  i                  ]                  ==                  '\n'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  14                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  16                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  19                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      iii                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      6                                    line length: 114                                      seven                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  10                                    line length: i                  eleven                                    line length: 428                  12                                    line length: i                  13                                    line length: 460                  xiv                                    line length: 1                  15                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  18                                    xix                                    20                                    Max line size: 1024                              

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

                                                                        one                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        four                                                        5                                    int                  main                  (                  void                  )                  {                                      half dozen                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  Aught                  )                  {                                      eight                                    perror                  (                  "Unable to open file!"                  );                                      ix                                    go out                  (                  ane                  );                  10                                    }                  11                                    12                                    // Read lines using POSIX part getline                  13                                    // This code won't work on Windows                  14                                    char                  *                  line                  =                  Aught                  ;                  15                                    size_t                  len                  =                  0                  ;                  xvi                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  18                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  19                                    }                  20                                    21                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline will resize the input buffer every bit necessary                  25                                    // the user needs to free the retention when non needed!                  26                                    }                              

Please note, how unproblematic is to utilise POSIX's 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 gratuitous the line buffer when you don't need information technology anymore. Also, calling getline more than one time will overwrite the line buffer, make a copy of the line content if yous need to keep it for further processing.

This is the outcome of running the to a higher place getline example on a Linux machine:

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      2                                    ~ $ ./t2                                      three                                    line length: 57                                      4                                    line length: 136                                      v                                    line length: 147                                      vi                                    line length: 114                                      vii                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  10                                    line length: one                  11                                    line length: 428                  12                                    line length: 1                  thirteen                                    line length: 460                  14                                    line length: ane                  xv                                    line length: 834                  sixteen                                    line length: 1                  17                                    line length: 821                  eighteen                                    xix                                    20                                    Max line size: 960                              

It is interesting to annotation, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If y'all run the same lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the different means in which getline is implemented on dissimilar Unix like systems.

Every bit mentioned before, getline is non present in the C standard library. It could be an interesting do to implement a portable version of this part. The thought here is non to implement the virtually performant version of getline, but rather to implement a simple replacement for non POSIX systems.

We are going to take the to a higher place instance and supervene upon the POSIX'due south getline version with our ain implementation, say my_getline. Apparently, if y'all are on a POSIX organization, you should utilize the version provided by the operating organisation, which was tested past countless users and tuned for optimal functioning.

The POSIX getline office 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 blazon, usually a 64 $.25 signed integer, this is how we are going to declare our version:

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

In principle we are going to implement the role using the same arroyo as in one 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 graphic symbol:

                                                                        1                                    // This will simply have event on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      3                                    #define _CRT_SECURE_NO_WARNINGS i                                      four                                    #define restrict __restrict                                      5                                    

0 Response to "C Read Character Length of Line From File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel