Hello World!
I am trying to read in a list of numbers from a file and put it into an array. Please i would be unbelievably grateful if you could tell me whats wrong with my code. This is my code:
#include <stdio.h>
FILE *fi;
FILE *fo;
int main()
{
float a[20];
int i, n, x, z;
float limit;
char line[50];
char letter[5];
i=0;
fi = fopen("input.txt", "r");
fo = fopen("output.txt", "w");
while (fgets(line,20,fi)!=NULL)
{
sscanf(line,"%3f2", &limit);
a[i] = limit;
i++;
}
printf("How many numbers would you like to input? ");
scanf("%3d2", &x);
for (n = 0; n < x; n++)
{
printf("%3f2\n", a[n]);
}
fclose(fi);
fclose(fo);
return 0;
}
This is the contents of input.txt:
123.34
342.54
243.43
And this is the output i get:
123.0000002
342.0000002
243.0000002
So thank you, hope to hear from you soon!