I have to store each data in an array. How do I read these data from a file?
120 5.0000000000000000E-01 -5.0000000000000000E-01 5.0000000000000000E-01
5.0000000000000000E-01 -5.0000000000000000E-01 -5.0000000000000000E-01
5.0000000000000000E-01 -5.0000000000000000E-01 1.6666666666999999E-01
5.0000000000000000E-01 -5.0000000000000000E-01 -1.6666666666999999E-01
-5.0000000000000000E-01
The data is mixture of integers, floating points and exponentials. The spaces between consecutive data are not constant, hence I cannot use a straightforward fscanf(). I also have to change them to integer hence, I cannot find alternative to fscanf() as I can specify the type specifier as %e in fscanf() argument and later change them to integer. I have tried fgetc() as well. Please show me a way.
edit.
To use fscanf() I need to have constant number of spaces or commas or anything between consecutive data. Here, the number of spaces between each data are not constant. So, I need to also implement a space checker in between.That's why I used fgetc() at a part.
#include<stdio.h>
int main()
{
int i=0,c,a[13];
FILE *fp;
fp=fopen("test.txt","r");
if(fp==NULL)
{
printf("Error");
}
else
{
i=0;
while(1)
{
c=fgetc(fp);
//printf("\nc = %c",c);
if(feof(fp))
{
break;
}
else if(c!=" ")
{
fscanf(fp,"%d",&a[i]);
printf("%d\n",a[i]);
i++;
}
}
}
fclose(fp);
return 0;
}
The data file is 2 m.b. I have just posted a part of it. Other parts have floating points, which are not exponentials like here.