5 Replies - 326 Views - Last Post: 23 October 2019 - 06:04 AM Rate Topic: -----

#1 Newbee20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 19

File position pointer?

Posted 22 October 2019 - 06:18 PM

Would someone please explain how to read multiple lines from a file one at a time. The following code reads the 1st line but how do I read the next lines? What is the mechanism that keeps track of the next line to read / address pointer? In the ascii file in question all lines end with 0D 0A / CR-LF.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[])
{
   
    char c[256];
    
    FILE *fr;

    
    fr= fopen("rod.gin", "r");  
    
    fscanf(fr,"%[^\n]", c);
    printf("Data from the file:\n%s", c);
    
    fclose(fr);
    while(1)
    {};
    
    return 0;


Is This A Good Question/Topic? 0
  • +

Replies To: File position pointer?

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,247
  • Joined: 05-May 12

Re: File position pointer?

Posted 22 October 2019 - 06:44 PM

Use a while loop that calls getline(). The function in knows how to read a complete line and eats the line break characters.
Was This Post Helpful? 0
  • +
  • -

#3 Newbee20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 19

Re: File position pointer?

Posted 22 October 2019 - 07:04 PM

My program is written in C not C++. Is there a EZ C solution or will getline work in C?

This post has been edited by Skydiver: 22 October 2019 - 08:10 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,247
  • Joined: 05-May 12

Re: File position pointer?

Posted 22 October 2019 - 08:11 PM

getline() exists in C. See man page.

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: File position pointer?

Posted 22 October 2019 - 08:23 PM

Actually in C getline() is not a standard C function and may not be available on all systems, even though it is a Posix standard function.

From your link above:

Quote

CONFORMING TO top

Both getline() and getdelim() were originally GNU extensions. They
were standardized in POSIX.1-2008.


Since it looks like the OP is on Windows getline() will probably not be available.

Quote

In the ascii file in question all lines end with 0D 0A / CR-LF.

And note to the OP line endings is system specific.


However you can use fgets() inside a loop to read multiple lines into an array of strings.

Be careful with your fscanf() function when retrieving strings, always use the correct optional width modifier to prevent overflowing your array.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Newbee20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-October 19

Re: File position pointer?

Posted 23 October 2019 - 06:04 AM

Thanks much for the replies.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1