By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,142 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,142 IT Pros & Developers. It's quick & easy.

extracting integers and floats from a file

P: 5
Hello,
I am trying to write a program that reads the first line of a variable size file i.e.
77 2.45 3 1.75
2 3.78 5 8.96 11 23.56
3 5.68 3 1.58 7 5.55

and return an array with its corresponding value. i.e. A[77] =2.45 A[3]=1.75.
then read the rest of the lines in the file the same way.
here is my program:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. float A[4000];
  5. FILE * pFile;
  6.  
  7. int main ()
  8. {
  9.  
  10. for (int i = 1; i < 4000; i++)
  11. int c;
  12. pFile=fopen ("test.txt","r");
  13. if (pFile)
  14. {
  15. do {
  16.  
  17. c = fgetc (pFile);
  18.  
  19. fscanf(pFile,"%d %f",i, &A[i]);
  20. i = A[i];
  21. printf("%d %2.5f\n",i, A[i]);
  22.  
  23. } while (c != EOF);
  24. }
  25. fclose (pFile);
  26.  
  27. }
  28.  
  29. return 0;
  30. }
  31.  
but I have problem running the above.
Please Help.
Jun 2 '10 #1
Share this Question
Share on Google+
6 Replies

P: 13
There are many flaws in your programm.
line 18: when you use fgetc, it will increment the file pointer then you do fscanf which expects first argument as interger. since file poiter is increment the value it will fetch is an float. and in such cases result is undefined.
line 20 : you should give &i instead of i.
line 21 : Should properly typecast values.
Jun 2 '10 #2

P: 5
@mayankarpit
Thank you mayan for your valuable inputs, very much appreciated.
I have swiched the order of fgetc and fscanf in the program and seems to work for getting the integer values from the file but not all the floats numbers as I am getting only one value from the file, any idea why.
Any input would help greatly :)
Jun 2 '10 #3

P: 13
@Bytes21
post the updated code
Jun 2 '10 #4

P: 5
@mayankarpit
Thanks for your quick reply. Here is the updated code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
float A[4000];
FILE * pFile;

int main ()
{

for (int i = 1; i < 5; i++)
{
int c;
pFile=fopen ("test.txt","r");
if (pFile)
{
do {
fscanf(pFile,"%d %f",&i, &A[i]);
c = fgetc (pFile);
//i = A[i];
printf("%d %2.5f\n",i, A[i]);

} while (c != EOF);
}
fclose (pFile);

}

return 0;
}
And below is sample of output that I get using the same data file posted earlier.
77 0.00000
3 0.00000
2 0.00000
5 0.00000
11 0.00000
3 3.78000
3 1.58000
7 0.00000
7 0.00000
Press any key to continue . . .

Thank you so much
Jun 2 '10 #5

P: 13
Hope this helps

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
float A[4000] = {0};
FILE *pFile;

int main ()
{
int c, i = 0;
pFile=fopen ("text.txt","r");
if (!pFile)
{
printf("failed to open file\n");
exit (1);
}
do
{
fscanf(pFile, "%d", &i);
fgetc(pFile);
fscanf(pFile, "%f" ,&A[i]);
printf("%d %f\n",i ,A[i]);
}while(fgetc(pFile) != EOF);
return 0;
}
Jun 2 '10 #6

P: 5
@mayankarpit
Great Help, thank you ever so much mayan.
Jun 2 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.