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!

sscanf(line,"%3f2", &limit); remove red part scanf("%3d2", &x); do same here. printf("%3f2\n", a[n]); substitute for %.2f\n

Thank you SO much Aia, you're a legend!

Seem my problem almost similar, but more complicated.

what if my input.txt consist of many column, than for every element there we want
to evaluate the value into new value?

for exampe the input.txt look like this:

123.34 1.02 2.02 3.04
342.54 0.01 12.3 0.19
243.43 0.99 11.23 1.345

thank you in advanced

what if my input.txt consist of many column, than for every element there we want
to evaluate the value into new value?

Not quite sure what you want to do with every floating point after been read. However, if you know how many columns the file is format into, all that you need to do is to add more specifiers to the function sscanf().
Assuming four columns: sscanf( string_holder, "%f%f%f%f", &column1, &column2, &column3, &column4 );

Ups sorry, if i did not well enough explain my problem. I will try it now:

Suppose i has the data file,
"array.dat", that contain like this:

100.01 8.02 7011.03 1.04 7.05
201.01 9.02 203.03 2.07 2.05
201.01 9.02 801.03 2.07 2.05
445.81 2.09 9090.04 5.09 901

the data should read first, and using some function
the data will consider as input (here as x), using this example
simple function: y=2.x-5,
every data point here should changed into new value,
than the result print into new file.

thanks a lot in advanced

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.