| | |
reading data from text file using fgets and sscanf
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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
1 3 5.6 3 2.1 4
2 2 3 4.2 1
here is what i have now
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> void main() { int i, n=1; int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0,n10=0; char c[10]; FILE *f1; f1=fopen("textfile.txt","r"); while(fgets(c,10,f1)!=NULL) { sscanf(c,"%d %d %d %d %d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10); printf("%d %d %d %d %d %d %d %d %d %d\n",n1,n2,n3,n4,n5,n6,n7,n8,n9,n10); getch(); }
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
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.
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.
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
Thanks for the input.
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...
•
•
•
•
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.
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
You've defined
And see this about
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
Well, i think, this might be a simpler way to solve your problem. I took an array of inetegers to be populated.
Hope it helps you out..
C Syntax (Toggle Plain Text)
int main(void) { FILE *fp; char buff[100]; int i = 0; int var[100]; fp = fopen("./test.txt","r"); while(fgets(buff,sizeof(buff),fp) { for(i=0;i<strlen(buff);i++) { if(isdigit(buff[i])) { var[i] = buff[i] - 48; printf("%d",var[i]); } } } printf("\n"); return 0; }
Hope it helps you out..
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....
ASCII 48 = 0 and so on....
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; #define size 10 int main() { int no[size], temp=0, i; FILE *fp=fopen("textfile.txt","r"); int ch=fgetc(fp); for(i=0; i<size; ch=fgetc(fp)) { if(ch>='0' && ch<='9') //taking only no temp=(temp*10)+(ch-48); else if(temp!=0) { no[i]=temp; printf("%d ", no[i]); if(ch==EOF) break; i++; temp=0; } } system("pause"); return 0; }
Last edited by Rhohitman; Jan 23rd, 2009 at 11:49 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Converting number to its alphabetic equivalent.
- Next Thread: MISTAKE
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






