7 Replies - 514 Views - Last Post: 20 August 2016 - 08:32 AM Rate Topic: -----

#1 tabado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-January 16

how do I fgets ALL the lines in the file and not only 1 line?

Posted 19 August 2016 - 11:29 PM

I am trying to scan all the lines in the textfile and then use fscanf to get info from the fgets line but fgets only gives me 1 of the 2 lines. Why?
for(int i=0;i<2;i++){
 while(fgets(persons, 100, fp)) {
   fscanf(fp, "%s %s", test[i], persons[i].name);
 }
}



This post has been edited by tabado: 19 August 2016 - 11:29 PM

Is This A Good Question/Topic? 0
  • +

Replies To: how do I fgets ALL the lines in the file and not only 1 line?

#2 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 05:42 AM

Well first fgets() stops processing the stream when it encounters the new line character. If you want to process more lines you need a loop.

Second fscanf() also reads from a stream (file) not from a string. If you want to parse a string retrieved by fgets() you need to use sscanf().

Third never use a function that doesn't limit the number of characters it will try to retrieve into a C-string. This is a buffer overflow waiting to happen, With the scanf() series of functions you need to use the proper width specifier to avoid the possibility of buffer overruns.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 tabado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-January 16

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 06:17 AM

Thank you i switched to sscanf() instead.
I dont get it, I use a forloop but it doesnt work? When there is a newline in the textfile, what should then happen? Obv it should keep scanning the nextline but isnt that what I am doing here?


do{
for(int i=0;i<2;i++){
 ch=fgetc(fp);
 while(fgets(persons, 100, fp)) {
   sscanf(persons, "%s %s", test[i], persons[i].name)
 }
}while(ch !=EOF);





This post has been edited by tabado: 20 August 2016 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 06:57 AM

What is the purpose of the for() loop?

What is the purpose of that fgetc() call?

What does your input file look like?

Don't forget to use the proper width specifier to avoid possible buffer overflow errors with your scanf().

Also please post a small complete program that illustrates the problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 tabado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-January 16

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 07:06 AM

I use the for loop to get the i for where to store the info from sscanf in persons struct. I was thinking that i = the line that are being scanned at the moment.
I used fgetc to end the loop when EOF but obv I dont need that when I have the forloop.
It only scans 1 of the lines.

Okay I will try to make a small full example of it in a sec


for(int i=0;i<2;i++){
 while(fgets(persons, sizeof(persons), fp)) 
   sscanf(persons, "%s %s", test[i], persons[i].name);
 }




This post has been edited by tabado: 20 August 2016 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 07:11 AM

Don't forget to post a sample of your input file when you post the small complete program that illustrates your problem. Your snippets are not providing enough information.

By the way you don't need that for() loop at all.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 tabado   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-January 16

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 07:47 AM

I got it to work. I used a buffer to store the lines then using malloc now I only have to sscanf the array

This post has been edited by tabado: 20 August 2016 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: how do I fgets ALL the lines in the file and not only 1 line?

Posted 20 August 2016 - 08:32 AM

Why are you using malloc()?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1