reading data from text file using fgets and sscanf

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 2
Reputation: encee is an unknown quantity at this point 
Solved Threads: 0
encee encee is offline Offline
Newbie Poster

reading data from text file using fgets and sscanf

 
0
  #1
Jan 22nd, 2009
i am trying to read a text file like this and store each number as an integer (without the decimals) and print them

1 3 5.6 3 2.1 4
2 2 3 4.2 1

here is what i have now

  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4.  
  5. void main()
  6. {
  7. int i, n=1;
  8. int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0,n10=0;
  9. char c[10];
  10. FILE *f1;
  11. f1=fopen("textfile.txt","r");
  12. while(fgets(c,10,f1)!=NULL)
  13. {
  14. sscanf(c,"%d %d %d %d %d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10);
  15. printf("%d %d %d %d %d %d %d %d %d %d\n",n1,n2,n3,n4,n5,n6,n7,n8,n9,n10);
  16. getch();
  17. }

but the output i get is always

1 3 5 0 0 0 0 0 0 0
2 2 3 4 0 0 0 0 0 0

its basically ignoring everything else once the sscanf encounters a decimal

Any suggestions??
Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: reading data from text file using fgets and sscanf

 
0
  #2
Jan 22nd, 2009
1. main returns int.
2. a 10 character buffer is nowhere near long enough to store 10 integers expressed as a string.

I would suggest
char c[BUFSIZ];
and update the fgets call accordingly.

Checking the return result of sscanf would be a good idea as well.

> its basically ignoring everything else once the sscanf encounters a decimal
Well what do you want to happen?
%d is for ints. If you want decimals, then use %f and adjust the data types.
Last edited by Salem; Jan 22nd, 2009 at 2:30 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: encee is an unknown quantity at this point 
Solved Threads: 0
encee encee is offline Offline
Newbie Poster

Re: reading data from text file using fgets and sscanf

 
0
  #3
Jan 22nd, 2009
Thanks for the input.

Originally Posted by Salem View Post
1. main returns int.
My bad, i almost alyways use int main

2. a 10 character buffer is nowhere near long enough to store 10 integers expressed as a string.

I've tried it with even a 100 character buffer with no difference in results. I will try BUFSIZ also..

I would suggest
char c[BUFSIZ];
and update the fgets call accordingly.

Checking the return result of sscanf would be a good idea as well.

> its basically ignoring everything else once the sscanf encounters a decimal
Well what do you want to happen?
%d is for ints. If you want decimals, then use %f and adjust the data types.
But if I assign a floating number as an integer, won't it implicitly convert it to int and move to the next character? However, it ignores ALL VALUES in that line of text after the decimal, which is what I find weird. I've also tried with floating point with no change in results...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading data from text file using fgets and sscanf

 
0
  #4
Jan 22nd, 2009
That's how sscanf works. A decimal is illegal for integers so the translation ends. You'll have to reconsider how you read/translate the file.

Also, consider these problems:

see this about void main()
You've defined c[] to be 10 characters. How many characters are in the first line for textfile.txt?

And see this about getch()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: reading data from text file using fgets and sscanf

 
0
  #5
Jan 22nd, 2009
Well if you want to ignore dots, then perhaps
"%d%*[^0-9]"
repeated 10 times
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: reading data from text file using fgets and sscanf

 
0
  #6
Jan 22nd, 2009
Well, i think, this might be a simpler way to solve your problem. I took an array of inetegers to be populated.

  1. int main(void)
  2. {
  3. FILE *fp;
  4. char buff[100];
  5. int i = 0;
  6. int var[100];
  7.  
  8. fp = fopen("./test.txt","r");
  9.  
  10. while(fgets(buff,sizeof(buff),fp)
  11. {
  12. for(i=0;i<strlen(buff);i++)
  13. {
  14. if(isdigit(buff[i]))
  15. {
  16. var[i] = buff[i] - 48;
  17. printf("%d",var[i]);
  18. }
  19. }
  20. }
  21.  
  22. printf("\n");
  23. return 0;
  24. }

Hope it helps you out..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: reading data from text file using fgets and sscanf

 
0
  #7
Jan 23rd, 2009
try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.

ASCII 48 = 0 and so on....

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. #define size 10
  5.  
  6. int main()
  7. {
  8. int no[size], temp=0, i;
  9.  
  10. FILE *fp=fopen("textfile.txt","r");
  11. int ch=fgetc(fp);
  12. for(i=0; i<size; ch=fgetc(fp))
  13. {
  14. if(ch>='0' && ch<='9') //taking only no
  15. temp=(temp*10)+(ch-48);
  16. else if(temp!=0)
  17. {
  18. no[i]=temp;
  19. printf("%d ", no[i]);
  20. if(ch==EOF) break;
  21. i++;
  22. temp=0;
  23. }
  24. }
  25.  
  26. system("pause");
  27. return 0;
  28. }
Last edited by Rhohitman; Jan 23rd, 2009 at 11:49 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading data from text file using fgets and sscanf

 
0
  #8
Jan 24th, 2009
Originally Posted by Rhohitman View Post
try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.
What?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 44
Reputation: me_ansh is an unknown quantity at this point 
Solved Threads: 5
me_ansh me_ansh is offline Offline
Light Poster

Re: reading data from text file using fgets and sscanf

 
0
  #9
Jan 25th, 2009
hope you got the answer to your question....if yes than please acknowledge and help us closing the thread...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC