How to Read Entire Line in C++ From Stdin

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

In this commodity, I volition bear witness you how to read a text file line by line in C using the standard C role fgets and the POSIX getline office. At the terminate of the commodity, I will write a portable implementation of the getline part that can be used with whatever standard C compiler.

Reading a file line by line is a trivial trouble in many programming languages, but not 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.

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

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

                                                                        1                                    #include                  <stdio.h>                                                        ii                                    #include                  <stdlib.h>                                                        three                                                        4                                    int                  main                  (                  void                  )                  {                                      5                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  Nix                  )                  {                                      seven                                    perror                  (                  "Unable to open file!"                  );                                      8                                    go out                  (                  1                  );                                      ix                                    }                  ten                                    11                                    char                  chunk                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  Goose egg                  )                  {                  14                                    fputs                  (                  chunk                  ,                  stdout                  );                  fifteen                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // marker string used to show where the content of the chunk assortment has ended                  xvi                                    }                  17                                    eighteen                                    fclose                  (                  fp                  );                  xix                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0                                      2                                    ~ $ ./t0                                      3                                    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 |*                                      ix                                    dignissim molestie.                  ten                                    |*                              

The code prints the content of the clamper array, as filled after every call to fgets, and a marking string.

If yous picket advisedly, by scrolling the above text snippet to the correct, you lot tin can see that the output was truncated to 127 characters per line of text. This was expected considering our code tin can store an entire line from the original text file only if the line tin fit inside our clamper array.

What if you need to have the entire line of text available for further processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until we find the finish of line character.

Permit's offset by creating a line buffer that will store the chunks of text, initially this will have the same length as the clamper array:

                                                                        1                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        iv                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    // ...                                      8                                                        9                                    char                  chunk                  [                  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                  ==                  Zip                  )                  {                  xv                                    perror                  (                  "Unable to allocate memory for the line buffer."                  );                  16                                    exit                  (                  one                  );                  17                                    }                  18                                    19                                    // "Empty" the string                  xx                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

Next, we are going to append the content of the clamper assortment to the terminate of the line string, until nosotros find the terminate of line character. If necessary, we'll resize the line buffer:

                                                                        1                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    // ...                                      vii                                                        8                                    // "Empty" the cord                                      9                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    xi                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Nil                  )                  {                  12                                    // Resize the line buffer if necessary                  xiii                                    size_t                  len_used                  =                  strlen                  (                  line                  );                  xiv                                    size_t                  chunk_used                  =                  strlen                  (                  chunk                  );                  15                                    xvi                                    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                                    free                  (                  line                  );                  21                                    exit                  (                  1                  );                  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                                    // Check if line contains '\n', if yep process the line of text                  30                                    if                  (                  line                  [                  len_used                  -                  ane                  ]                  ==                  '\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                                    free                  (                  line                  );                  twoscore                                    41                                    printf                  (                  "                  \n\north                  Max line size: %zd                  \northward                  "                  ,                  len                  );                  42                                    }                              

Please annotation, that in the to a higher place code, every time the line buffer needs to exist resized its capacity is doubled.

This is the outcome of running the higher up code on my car. For brevity, I kept only the showtime lines of output:

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      three                                    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 condimentum.                                      6                                    |*                                      seven                                    Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie.                                      viii                                    |*                                      nine                                    Aliquam erat volutpat. Mauris dignissim augue air conditioning purus placerat scelerisque. Donec eleifend ut nibh eu elementum.                  x                                    |*                              

You can see that, this time, we tin can print total lines of text and not fixed length chunks similar in the initial approach.

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

                                                                        1                                    // ...                                      2                                                        three                                    int                  main                  (                  void                  )                  {                                      4                                    // ...                                      5                                                        6                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  NULL                  )                  {                                      7                                                        8                                    // ...                                      ix                                    10                                    // Check if line contains '\north', if yep process the line of text                  eleven                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\northward'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \northward                  "                  ,                  len_used                  );                  xiii                                    // "Empty" the line buffer                  fourteen                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  sixteen                                    }                  17                                    xviii                                    fclose                  (                  fp                  );                  19                                    free                  (                  line                  );                  twenty                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

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

                                                                        1                                    ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1                                      2                                    ~ $ ./t1                                      3                                    line length: 57                                      iv                                    line length: 136                                      v                                    line length: 147                                      half dozen                                    line length: 114                                      seven                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  x                                    line length: one                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  fourteen                                    line length: ane                  15                                    line length: 834                  16                                    line length: one                  17                                    line length: 821                  eighteen                                    19                                    20                                    Max line size: 1024                              

In the next instance, I will prove yous how to utilise the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent part, and then you won't be able to easily test this example on a Windows system. However, you lot should be able to test it if you are using Cygwin or Windows Subsystem for Linux.

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      vii                                    if                  (                  fp                  ==                  NULL                  )                  {                                      8                                    perror                  (                  "Unable to open file!"                  );                                      9                                    exit                  (                  1                  );                  x                                    }                  11                                    12                                    // Read lines using POSIX part getline                  13                                    // This code won't work on Windows                  fourteen                                    char                  *                  line                  =                  Cipher                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  xviii                                    printf                  (                  "line length: %zd                  \north                  "                  ,                  strlen                  (                  line                  ));                  19                                    }                  twenty                                    21                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline will resize the input buffer as necessary                  25                                    // the user needs to gratis the retention when not needed!                  26                                    }                              

Please notation, how uncomplicated is to utilize POSIX'south 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 office.

When you employ getline, don't forget to free the line buffer when you don't demand it anymore. Also, calling getline more once will overwrite the line buffer, brand a copy of the line content if you demand to keep it for farther processing.

This is the result of running the to a higher place getline case on a Linux machine:

                                                                        one                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      ii                                    ~ $ ./t2                                      3                                    line length: 57                                      4                                    line length: 136                                      5                                    line length: 147                                      half-dozen                                    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: 1                  xv                                    line length: 834                  16                                    line length: 1                  17                                    line length: 821                  eighteen                                    19                                    20                                    Max line size: 960                              

It is interesting to note, that for this particular example the getline office on Linux resizes the line buffer to a max of 960 bytes. If yous run the same code on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on unlike Unix like systems.

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

We are going to have the to a higher place example and supplant the POSIX's getline version with our ain implementation, say my_getline. Obviously, if you are on a POSIX system, you lot should apply the version provided by the operating organisation, 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                  north                  ,                  FILE                  *                  restrict                  stream                  );                              

Since ssize_t is also a POSIX defined blazon, commonly a 64 bits signed integer, this is how we are going to declare our version:

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

                                                                        1                                    // This volition simply have effect on Windows with MSVC                                      two                                    #ifdef _MSC_VER                                      iii                                    #ascertain _CRT_SECURE_NO_WARNINGS 1                                      four                                    #define restrict __restrict                                      5                                    

Belum ada Komentar untuk "How to Read Entire Line in C++ From Stdin"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel