How to Read All the Lines of a Text File C++

Solarian Developer

My programming ramblings

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

Posted on April 3, 2019 past Paul

In this commodity, I will prove y'all how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the finish of the article, I will write a portable implementation of the getline office that can be used with whatever standard C compiler.

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

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

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

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                                        four                                    int                  primary                  (                  void                  )                  {                                      five                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      half-dozen                                    if                  (                  fp                  ==                  Zero                  )                  {                                      7                                    perror                  (                  "Unable to open file!"                  );                                      8                                    exit                  (                  one                  );                                      9                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    xiii                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  14                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marker cord used to show where the content of the chunk array has ended                  16                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  xix                                    }                              

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

                                                                        ane                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      2                                    ~ $ ./t0                                      iii                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit.                                      four                                    |*                                      five                                    Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|*                                      half dozen                                    imentum.                                      7                                    |*                                      viii                                    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 chunk assortment, as filled later every telephone call to fgets, and a marker cord.

If you spotter carefully, by scrolling the above text snippet to the right, you tin run into 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 inside our clamper array.

What if yous demand to take the unabridged 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 split up line buffer until we find the end of line character.

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

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        iv                                                        5                                    int                  principal                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      8                                                        9                                    char                  chunk                  [                  128                  ];                  10                                    11                                    // Shop the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  clamper                  );                  13                                    char                  *                  line                  =                  malloc                  (                  len                  );                  14                                    if                  (                  line                  ==                  NULL                  )                  {                  15                                    perror                  (                  "Unable to allocate memory for the line buffer."                  );                  xvi                                    exit                  (                  1                  );                  17                                    }                  eighteen                                    19                                    // "Empty" the string                  20                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

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

                                                                        i                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        four                                                        5                                    int                  main                  (                  void                  )                  {                                      half-dozen                                    // ...                                      7                                                        8                                    // "Empty" the cord                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  ten                                    eleven                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  12                                    // Resize the line buffer if necessary                  13                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  chunk                  );                  15                                    16                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  2                  ;                  xviii                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  Cypher                  )                  {                  xix                                    perror                  (                  "Unable to reallocate retentiveness for the line buffer."                  );                  20                                    free                  (                  line                  );                  21                                    leave                  (                  1                  );                  22                                    }                  23                                    }                  24                                    25                                    // Copy the chunk to the terminate of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  clamper                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Cheque if line contains '\north', if yes procedure the line of text                  thirty                                    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                                    costless                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

Please note, that in the to a higher place lawmaking, every time the line buffer needs to be resized its capacity is doubled.

This is the issue of running the above code on my machine. For brevity, I kept but the kickoff lines of output:

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

Y'all tin can see that, this time, we can impress full lines of text and non fixed length chunks like in the initial approach.

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

                                                                        1                                    // ...                                      two                                                        iii                                    int                  main                  (                  void                  )                  {                                      four                                    // ...                                      5                                                        half-dozen                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Zilch                  )                  {                                      7                                                        8                                    // ...                                      nine                                    ten                                    // Cheque if line contains '\n', if yes procedure the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  ane                  ]                  ==                  '\northward'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  fourteen                                    line                  [                  0                  ]                  =                  '\0'                  ;                  fifteen                                    }                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  xix                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

This is the event of running the modified code on my auto:

                                                                        i                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      ii                                    ~ $ ./t1                                      3                                    line length: 57                                      4                                    line length: 136                                      v                                    line length: 147                                      six                                    line length: 114                                      7                                    line length: 112                                      viii                                    line length: 95                                      9                                    line length: 62                  10                                    line length: ane                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: 1                  xv                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  18                                    nineteen                                    20                                    Max line size: 1024                              

In the next example, I will show you lot how to employ the getline part bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't accept an equivalent function, so you won't be able to easily examination this example on a Windows organization. However, you should exist able to test it if you are using Cygwin or Windows Subsystem for Linux.

                                                                        one                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <cord.h>                                                        4                                                        v                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  NULL                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      9                                    go out                  (                  1                  );                  ten                                    }                  xi                                    12                                    // Read lines using POSIX part getline                  13                                    // This code won't piece of work on Windows                  14                                    char                  *                  line                  =                  NULL                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  18                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  nineteen                                    }                  20                                    21                                    printf                  (                  "                  \n\northward                  Max line size: %zd                  \due north                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    complimentary                  (                  line                  );                  // getline will resize the input buffer equally necessary                  25                                    // the user needs to free the retention when non needed!                  26                                    }                              

Please note, how simple is to use 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 role.

When you use getline, don't forget to free the line buffer when you don't need it anymore. Also, calling getline more once will overwrite the line buffer, brand a copy of the line content if yous need to continue information technology 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                                      2                                    ~ $ ./t2                                      3                                    line length: 57                                      4                                    line length: 136                                      five                                    line length: 147                                      6                                    line length: 114                                      7                                    line length: 112                                      viii                                    line length: 95                                      nine                                    line length: 62                  10                                    line length: 1                  11                                    line length: 428                  12                                    line length: 1                  thirteen                                    line length: 460                  14                                    line length: 1                  fifteen                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  18                                    19                                    20                                    Max line size: 960                              

It is interesting to note, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned code 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.

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

Nosotros are going to take the above example and replace the POSIX'southward getline version with our own implementation, say my_getline. Obviously, if yous are on a POSIX system, you should use the version provided by the operating system, 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 too a POSIX defined type, commonly a 64 bits signed integer, this is how we are going to declare our version:

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

In principle nosotros are going to implement the part using the same approach as in one of the above examples, where I've divers a line buffer and kept copying chunks of text in the buffer until we plant the end of line grapheme:

                                                                        one                                    // This volition only have issue on Windows with MSVC                                      2                                    #ifdef _MSC_VER                                      3                                    #define _CRT_SECURE_NO_WARNINGS one                                      4                                    #define restrict __restrict                                      5                                    

0 Response to "How to Read All the Lines of a Text File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel