How to Read One Line at a Time From a File in C++

Solarian Programmer

My programming ramblings

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

Posted on Apr 3, 2019 by Paul

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

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

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

Let'southward outset with a uncomplicated example of using fgets to read chunks from a text file. :

                                                                        ane                                    #include                  <stdio.h>                                                        ii                                    #include                  <stdlib.h>                                                        three                                                        four                                    int                  main                  (                  void                  )                  {                                      v                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      7                                    perror                  (                  "Unable to open file!"                  );                                      eight                                    go out                  (                  1                  );                                      nine                                    }                  10                                    11                                    char                  chunk                  [                  128                  ];                  12                                    thirteen                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Null                  )                  {                  14                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marker string used to show where the content of the clamper array has ended                  sixteen                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  19                                    }                              

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

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

The code prints the content of the clamper array, every bit filled afterwards every call to fgets, and a marker cord.

If y'all lookout man carefully, past scrolling the above text snippet to the right, y'all tin see that the output was truncated to 127 characters per line of text. This was expected because our code can store an entire line from the original text file but if the line can fit inside our chunk array.

What if you lot need to accept the entire line of text available for further processing and non a slice of line ? A possible solution is to copy or concatenate chunks of text in a split line buffer until nosotros detect the cease of line graphic symbol.

Let's offset by creating a line buffer that will shop the chunks of text, initially this will have the same length equally the chunk array:

                                                                        i                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        five                                    int                  main                  (                  void                  )                  {                                      vi                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      8                                                        9                                    char                  clamper                  [                  128                  ];                  ten                                    11                                    // Store the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  chunk                  );                  xiii                                    char                  *                  line                  =                  malloc                  (                  len                  );                  fourteen                                    if                  (                  line                  ==                  Nada                  )                  {                  15                                    perror                  (                  "Unable to allocate retentiveness for the line buffer."                  );                  sixteen                                    go out                  (                  1                  );                  17                                    }                  18                                    xix                                    // "Empty" the string                  20                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

Next, we are going to suspend the content of the chunk assortment to the stop of the line cord, until nosotros find the stop of line grapheme. If necessary, we'll resize the line buffer:

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        three                                    #include                  <cord.h>                                                        iv                                                        five                                    int                  primary                  (                  void                  )                  {                                      half-dozen                                    // ...                                      seven                                                        viii                                    // "Empty" the string                                      ix                                    line                  [                  0                  ]                  =                  '\0'                  ;                  ten                                    11                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Cypher                  )                  {                  12                                    // Resize the line buffer if necessary                  xiii                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  14                                    size_t                  chunk_used                  =                  strlen                  (                  clamper                  );                  15                                    16                                    if                  (                  len                  -                  len_used                  <                  chunk_used                  )                  {                  17                                    len                  *=                  ii                  ;                  18                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  Nada                  )                  {                  xix                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  twenty                                    free                  (                  line                  );                  21                                    exit                  (                  i                  );                  22                                    }                  23                                    }                  24                                    25                                    // Re-create the clamper to the finish of the line buffer                  26                                    strncpy                  (                  line                  +                  len_used                  ,                  clamper                  ,                  len                  -                  len_used                  );                  27                                    len_used                  +=                  chunk_used                  ;                  28                                    29                                    // Check if line contains '\north', if yes procedure the line of text                  30                                    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                                    costless                  (                  line                  );                  40                                    41                                    printf                  (                  "                  \due north\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

Please annotation, that in the to a higher place code, every time 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 machine. For brevity, I kept only the first lines of output:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      two                                    ~ $ ./t1                                      3                                    Lorem ipsum dolor sit down amet, consectetur adipiscing elit.                                      four                                    |*                                      5                                    Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum.                                      6                                    |*                                      seven                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.                                      8                                    |*                                      9                                    Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh eu elementum.                  10                                    |*                              

Yous tin meet that, this time, we can impress full lines of text and non stock-still length chunks like in the initial approach.

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

                                                                        1                                    // ...                                      two                                                        3                                    int                  primary                  (                  void                  )                  {                                      four                                    // ...                                      5                                                        vi                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  NULL                  )                  {                                      7                                                        8                                    // ...                                      9                                    10                                    // Cheque if line contains '\due north', if yes procedure the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\north'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  14                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  nineteen                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      ii                                    ~ $ ./t1                                      3                                    line length: 57                                      iv                                    line length: 136                                      5                                    line length: 147                                      6                                    line length: 114                                      7                                    line length: 112                                      8                                    line length: 95                                      ix                                    line length: 62                  x                                    line length: i                  xi                                    line length: 428                  12                                    line length: i                  13                                    line length: 460                  14                                    line length: 1                  15                                    line length: 834                  xvi                                    line length: ane                  17                                    line length: 821                  18                                    19                                    20                                    Max line size: 1024                              

In the side by side example, I will prove yous how to apply the getline function available on POSIX systems similar Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent role, and so you won't exist able to hands examination this case on a Windows organization. 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                  <cord.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      six                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  NULL                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      nine                                    go out                  (                  ane                  );                  ten                                    }                  xi                                    12                                    // Read lines using POSIX function getline                  13                                    // This code won't piece of work on Windows                  xiv                                    char                  *                  line                  =                  Nothing                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  i                  )                  {                  xviii                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  19                                    }                  twenty                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \due north                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline will resize the input buffer as necessary                  25                                    // the user needs to free the memory when non needed!                  26                                    }                              

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

When you apply getline, don't forget to free the line buffer when you lot don't need it anymore. Besides, calling getline more than than one time will overwrite the line buffer, brand a copy of the line content if you need to keep it for further processing.

This is the result 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                                      four                                    line length: 136                                      5                                    line length: 147                                      six                                    line length: 114                                      7                                    line length: 112                                      8                                    line length: 95                                      nine                                    line length: 62                  10                                    line length: 1                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: ane                  fifteen                                    line length: 834                  16                                    line length: ane                  17                                    line length: 821                  eighteen                                    nineteen                                    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 code on macOS the line buffer is resized to 1024 bytes. This is due to the unlike ways in which getline is implemented on different Unix like systems.

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

We are going to have the to a higher place case and replace the POSIX's getline version with our own implementation, say my_getline. Manifestly, if you lot are on a POSIX arrangement, you lot should utilise the version provided past the operating system, which was tested by countless users and tuned for optimal functioning.

The POSIX getline function has this signature:

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

Since ssize_t is also a POSIX defined type, usually a 64 bits signed integer, this is how nosotros 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 part using the same arroyo as in 1 of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros establish the finish of line character:

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

0 Response to "How to Read One Line at a Time From a File in C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel