Reading Line by Line Using File Pointer C++

Solarian Programmer

My programming ramblings

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

Posted on Apr 3, 2019 by Paul

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

Reading a file line past line is a piddling problem 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 accelerate how long a line of text could be.

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

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

                                                                        i                                    #include                  <stdio.h>                                                        ii                                    #include                  <stdlib.h>                                                        3                                                        four                                    int                  main                  (                  void                  )                  {                                      five                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      6                                    if                  (                  fp                  ==                  NULL                  )                  {                                      seven                                    perror                  (                  "Unable to open file!"                  );                                      8                                    exit                  (                  1                  );                                      9                                    }                  10                                    xi                                    char                  clamper                  [                  128                  ];                  12                                    13                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  NULL                  )                  {                  fourteen                                    fputs                  (                  chunk                  ,                  stdout                  );                  15                                    fputs                  (                  "|*                  \n                  "                  ,                  stdout                  );                  // mark string used to show where the content of the clamper assortment has ended                  sixteen                                    }                  17                                    xviii                                    fclose                  (                  fp                  );                  xix                                    }                              

For testing the code I've used a uncomplicated dummy file, lorem.txt. This is a slice from the output of the in a higher place program on my motorcar:

                                                                        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|*                                      vi                                    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 chunk array, equally filled later every call to fgets, and a marking string.

If you watch advisedly, by scrolling the higher up text snippet to the right, y'all can 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 only if the line tin fit inside our chunk array.

What if you lot need to accept the entire line of text bachelor for further processing and not a slice of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until we detect the terminate of line grapheme.

Let's showtime by creating a line buffer that will store the chunks of text, initially this will have the same length every bit the chunk array:

                                                                        one                                    #include                  <stdio.h>                                                        two                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        5                                    int                  main                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      seven                                    // ...                                      8                                                        9                                    char                  chunk                  [                  128                  ];                  ten                                    xi                                    // Store the chunks of text into a line buffer                  12                                    size_t                  len                  =                  sizeof                  (                  chunk                  );                  13                                    char                  *                  line                  =                  malloc                  (                  len                  );                  14                                    if                  (                  line                  ==                  NULL                  )                  {                  15                                    perror                  (                  "Unable to allocate memory for the line buffer."                  );                  xvi                                    go out                  (                  one                  );                  17                                    }                  18                                    19                                    // "Empty" the string                  20                                    line                  [                  0                  ]                  =                  '\0'                  ;                  21                                    22                                    // ...                  23                                    24                                    }                              

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

                                                                        1                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        iii                                    #include                  <string.h>                                                        4                                                        five                                    int                  primary                  (                  void                  )                  {                                      6                                    // ...                                      7                                                        8                                    // "Empty" the string                                      nine                                    line                  [                  0                  ]                  =                  '\0'                  ;                  10                                    11                                    while                  (                  fgets                  (                  chunk                  ,                  sizeof                  (                  chunk                  ),                  fp                  )                  !=                  Nix                  )                  {                  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                  *=                  ii                  ;                  eighteen                                    if                  ((                  line                  =                  realloc                  (                  line                  ,                  len                  ))                  ==                  NULL                  )                  {                  nineteen                                    perror                  (                  "Unable to reallocate memory for the line buffer."                  );                  twenty                                    complimentary                  (                  line                  );                  21                                    go out                  (                  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 '\north', if yep process the line of text                  30                                    if                  (                  line                  [                  len_used                  -                  i                  ]                  ==                  '\northward'                  )                  {                  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                  );                  xl                                    41                                    printf                  (                  "                  \n\n                  Max line size: %zd                  \n                  "                  ,                  len                  );                  42                                    }                              

Please note, that in the above code, every fourth dimension the line buffer needs to be resized its capacity is doubled.

This is the result of running the higher up code on my auto. For brevity, I kept just the outset lines of output:

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

You can see that, this fourth dimension, nosotros can impress full lines of text and not fixed length chunks like in the initial approach.

Let's alter the higher up code in gild to print the line length instead of the actual text:

                                                                        i                                    // ...                                      2                                                        3                                    int                  main                  (                  void                  )                  {                                      iv                                    // ...                                      5                                                        half dozen                                    while                  (                  fgets                  (                  clamper                  ,                  sizeof                  (                  clamper                  ),                  fp                  )                  !=                  NULL                  )                  {                                      seven                                                        eight                                    // ...                                      nine                                    10                                    // Check if line contains '\due north', if yep process the line of text                  11                                    if                  (                  line                  [                  len_used                  -                  1                  ]                  ==                  '\due north'                  )                  {                  12                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  len_used                  );                  13                                    // "Empty" the line buffer                  14                                    line                  [                  0                  ]                  =                  '\0'                  ;                  15                                    }                  16                                    }                  17                                    18                                    fclose                  (                  fp                  );                  19                                    free                  (                  line                  );                  20                                    21                                    printf                  (                  "                  \n\northward                  Max line size: %zd                  \n                  "                  ,                  len                  );                  22                                    }                              

This is the consequence 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                                      7                                    line length: 112                                      8                                    line length: 95                                      9                                    line length: 62                  ten                                    line length: i                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: one                  15                                    line length: 834                  sixteen                                    line length: 1                  17                                    line length: 821                  18                                    19                                    20                                    Max line size: 1024                              

In the next instance, I volition evidence you how to use the getline part available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so y'all won't be able to easily test this example on a Windows system. Even so, you should be able to test it if you are using Cygwin or Windows Subsystem for Linux.

                                                                        one                                    #include                  <stdio.h>                                                        2                                    #include                  <stdlib.h>                                                        3                                    #include                  <string.h>                                                        four                                                        five                                    int                  master                  (                  void                  )                  {                                      6                                    FILE                  *                  fp                  =                  fopen                  (                  "lorem.txt"                  ,                  "r"                  );                                      7                                    if                  (                  fp                  ==                  Null                  )                  {                                      viii                                    perror                  (                  "Unable to open file!"                  );                                      ix                                    go out                  (                  i                  );                  x                                    }                  11                                    12                                    // Read lines using POSIX function getline                  13                                    // This code won't work on Windows                  14                                    char                  *                  line                  =                  Zero                  ;                  15                                    size_t                  len                  =                  0                  ;                  16                                    17                                    while                  (                  getline                  (                  &                  line                  ,                  &                  len                  ,                  fp                  )                  !=                  -                  1                  )                  {                  eighteen                                    printf                  (                  "line length: %zd                  \n                  "                  ,                  strlen                  (                  line                  ));                  19                                    }                  twenty                                    21                                    printf                  (                  "                  \northward\n                  Max line size: %zd                  \north                  "                  ,                  len                  );                  22                                    23                                    fclose                  (                  fp                  );                  24                                    free                  (                  line                  );                  // getline will resize the input buffer as necessary                  25                                    // the user needs to costless the retention when not needed!                  26                                    }                              

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

When you use getline, don't forget to free the line buffer when you lot don't need it anymore. Also, calling getline more than once will overwrite the line buffer, make a copy of the line content if yous need to go along it for further processing.

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

                                                                        1                                    ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2                                      ii                                    ~ $ ./t2                                      3                                    line length: 57                                      4                                    line length: 136                                      five                                    line length: 147                                      6                                    line length: 114                                      7                                    line length: 112                                      eight                                    line length: 95                                      9                                    line length: 62                  ten                                    line length: 1                  11                                    line length: 428                  12                                    line length: 1                  13                                    line length: 460                  14                                    line length: 1                  15                                    line length: 834                  xvi                                    line length: 1                  17                                    line length: 821                  18                                    xix                                    xx                                    Max line size: 960                              

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

As mentioned before, getline is non nowadays in the C standard library. Information technology could be an interesting exercise to implement a portable version of this function. The thought here is not to implement the near performant version of getline, but rather to implement a elementary replacement for non POSIX systems.

We are going to take the above instance and replace the POSIX'due south getline version with our own implementation, say my_getline. Obviously, if you are on a POSIX organisation, you lot should use the version provided past 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                  n                  ,                  FILE                  *                  restrict                  stream                  );                              

Since ssize_t is too a POSIX defined blazon, unremarkably 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 function using the aforementioned arroyo equally in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line graphic symbol:

                                                                        i                                    // This will just have effect on Windows with MSVC                                      ii                                    #ifdef _MSC_VER                                      iii                                    #ascertain _CRT_SECURE_NO_WARNINGS 1                                      4                                    #define restrict __restrict                                      5                                    

0 Response to "Reading Line by Line Using File Pointer C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel