Hello all..
I'm fairly new to programming and am trying to get the following implemented. I have a .txt file which contains something like this...
1.34324 10202043
1.78458 19430437
1.93287 10545348
2.37432 21025745
...
etc..
Now initially what i need to do is, to read the first floating point number into a variable in this case 1.34324. Then I need to have an array of float that contains all the floats from the first column of the file.
Here is the code I have written but as you can see once i fscanf the first float into the variable t0, the array a[i] starts scanning from the second line and not the first as I would like it to. How can I make the file pointer go back to beginning of file.
Code:
#include<stdio.h>
int main()
{
FILE *fptr;
int i=0;
float t0;
int bin0;
float a[11];
int b[11];
fptr = fopen("flt_read.txt","r");
if(fptr == NULL){
printf("Error!! Cannot open file \n" );
return 1;}
else
{
printf("File opened successfully :) \n");
fscanf(fptr,"%f %d",&t0,&bin0);
printf("t0 = %f\n",t0);
while(!feof(fptr))
{
fscanf(fptr,"%f %d",&a[i],&b[i]);
printf("%f\n",a[i]);
++i;
}
}
return 0;
}
I have 2 other problems
1) In the output I see a negative floating point value that is different every time i execute the file. Almost like some error value. I don't know what this is..
2) Is there anyway to read just the first column without having to read the second one into an integer array. If i just say
Code:
fscanf(fptr"%f",&a[i])
it reads the entire line as a float and I don't want this..
Pls help.. Thanks in advance