fscanf, floats and the SD Card
BlackSoldierB
Posts: 45
I recently got a ActivityBot. I am trying to read a float from a text file on the SD Card, but it only read the value 0.0.
The file contains only the value 1.0.
What am I doing wrong?
The .c file and output are listed below.
The fprintf method and reading integers are working correct.
My .c file:
Console Output:
Starting
0.000000
The file contains only the value 1.0.
What am I doing wrong?
The .c file and output are listed below.
The fprintf method and reading integers are working correct.
My .c file:
#include "simpletools.h" int main(){ low(26); low(27); print("Starting\n"); high(26); // SD card pins on ActivityBot int DO = 22, CLK = 23, DI = 24, CS = 25; // Mount SD card sd_mount(DO, CLK, DI, CS); FILE* _file = fopen("test.txt", "r"); if (_file == NULL) { return 1; } while (!feof(_file)) { float f = -1; fscanf(_file, "%f", &f); print("%f\n", f); } high(27); return 0; }
Console Output:
Starting
0.000000
Comments
EDIT: However, it did print 0.0000, so it must know something about floating point. Try printing with %x instead of %f just to see if the number is non-zero. You could also try reading the file with fgets, and print out the string you get to see if it is reading it properly from the file.
Eric
It's good to go through the entire Learn curriculum to see what is possible with Simple Libraries. They are different from the standard C libraries and are designed just for the Learn tutorials where many concepts and programs are demonstrated. Standard C libraries can be used with Propeller, but because of memory issues and other details they can be very difficult to use for beginners. The Learn libraries are meant to provide the least amount of distraction for students and new teachers alike.
Please use either the function set print/sscan/fwrite/fread or the function set printf/sscanf/fprintf/fscanf. It makes no sense to mix them up because you end up using more memory. If you learned to mix things up from the learn.parallax.propeller-c site, then please let us know so that it can be fixed.
The reason to use print and friends is to avoid the unexpected code size explosion you end up getting with printf and you don't need to link the math library. The puts and printf functions can be smaller than print until you start adding the format specifiers. The print function will always yield the same size.
Functions print/scan/sscan/dscan/dprint, etc... can be used with fopen/fread/fwrite/fclose, and all handle floating point without the math library. They are documented in the Simple Library pages either in the SimpleIDE installation or online here.
I forgot to mention that i am using C++, but that doesn't seem to fix the problem (Only reduced the memory with a little).
I am using print instead of printf because for some reason print doesn't print to the terminal (Maybe i have to change the stdout?). The print function does work correct because if i try the code below it produces the correct output.
What is the 'simple' alternative of fscanf? First do a fread and then perform a sscan?
I am trying to make the code work first and then try to optimize the memory usage and reduce the libraries.
Regarding the modifer issue, my compiler is on "All warnings". This seems correct and doesn't produce any warnings.
This is my test code from visual studio, a c++ console application. This code does work correct, so something must differ in the fscanf function.
The Simple Libraries print and friends introduces a small delay to allow the terminal time to catch the serial port after the external loader does its job. There is no such delay for printf and friends, and seeing the output can be hit or miss - most often a miss. I recommend adding a delay or a getchar() call to the beginning of the program. The delay is not mentioned on the Learn site because the standard library functions are not mentioned. There really should be some standard library comparison notes there for experienced programmers.
Yes. Attached are two functional SimpleIDE project versions for comparison.
In your visual studio console application you have:
But in the Propeller code you have:
Note that the type of "f" is different in these two. "f" must be double. That is your fundamental problem.
There may also be an issue with print/printf (printf should work) but that's another matter.
I got an idea how it should work and i think i have to go with the simple version because it uses less memory.
EDIT: I am using fgets since it reads a line.
This is the code if anyone is interested in it.
Note: This produces a warning at print, format '%f' expects argument of type 'double', but argument 2 has type 'float'.