Is there an easy way to read data, expressed in engineering units, into a C program from a text file?
I have a file containing data that has a single column of numbers like this:
9.27848E-01
-3.89783E-01
Is there an easy way to read data, expressed in engineering units, into a C program from a text file?
I have a file containing data that has a single column of numbers like this:
9.27848E-01
-3.89783E-01
This is scientific notation. I would suggest you to
- open file with fopen (don't forget to check that the file pointer is not NULL)
- read data with fread into a char buffer (do that in a loop until you find EOF)
- every cell has a line of the data that are in file
- close the file with fclose
Welcome to the forum!
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
You can do this with scanf. Example
Code:double num; while ((scanf("%lf", &num) == 1)) printf("Got num: %.8f\n", num);
With fcanf you mean I suppose.
Better choice
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Open the file in text mode, then use fscanf with the "%f" modifier for storing the value in a float, or "%lf" for storing in a double.