943,822 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5543
  • C RSS
Jan 22nd, 2009
0

reading data from text file using fgets and sscanf

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
encee is offline Offline
2 posts
since Jan 2009
Jan 22nd, 2009
0

Re: reading data from text file using fgets and sscanf

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 22nd, 2009
0

Re: reading data from text file using fgets and sscanf

Thanks for the input.

Click to Expand / Collapse  Quote originally posted by Salem ...
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
encee is offline Offline
2 posts
since Jan 2009
Jan 22nd, 2009
0

Re: reading data from text file using fgets and sscanf

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()
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jan 22nd, 2009
0

Re: reading data from text file using fgets and sscanf

Well if you want to ignore dots, then perhaps
"%d%*[^0-9]"
repeated 10 times
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 22nd, 2009
0

Re: reading data from text file using fgets and sscanf

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..
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009
Jan 23rd, 2009
0

Re: reading data from text file using fgets and sscanf

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....

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Jan 24th, 2009
0

Re: reading data from text file using fgets and sscanf

Click to Expand / Collapse  Quote originally posted by Rhohitman ...
try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.
What?
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jan 25th, 2009
0

Re: reading data from text file using fgets and sscanf

hope you got the answer to your question....if yes than please acknowledge and help us closing the thread...
Reputation Points: 13
Solved Threads: 5
Light Poster
me_ansh is offline Offline
44 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Converting number to its alphabetic equivalent.
Next Thread in C Forum Timeline: MISTAKE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC