Read and write REAL scalars from and to file

I need to read and write REAL acalars from and to file. Please advise how to correct the following
blocks of code. 'Short' and 'REAL' are mandated by the modelling software that I use.

Thank you in advance.

John Weldon
__________________________

Read REAL scalar from file

void /*FUNCTION*/ rdrarr0( char *filnam, REAL narray )

short refi, reti;

file1 = fopen( filnam, "r+" );
assert( file1 != NULL );
refi = fscanf_s( file1, "%f \r\n", narray); <-----------
assert(refi != EOF);
reti = fclose(file1);
assert(reti != EOF);

Warning C4477 'fscanf_s': format string '%f' requires an argument of type 'float *', but variadic
argument 1 has type 'double'
_______________________________

Write REAL scalar to file

void /*FUNCTION*/ wtrarr0( char *filnam, REAL nscal )

short reti;

file1 = fopen( filnam, "w+" );
fprintf_s( "file1, %f \r\n", nscal ); <-----------
assert( file1 != NULL );
reti = fclose(file1);
assert(reti != EOF);

Warning C4024 'fprintf_s': different types for formal and actual parameter 2
Error C2440 'function': cannot convert from 'REAL' to 'const char *const
Warning C4133 'function': incompatible types - from 'char [13]' to 'FILE *const
Error (active) E0167 argument of type "REAL" is incompatible with parameter of type "const char
*const"
'Short' and 'REAL' are mandated by the modelling software that I use.


c++ has a fundamental 'short' integer, there is no 'Short'

As for 'REAL' c++ doesn't recognise what that data type is either and you will have to define/convert/export (via your separate modelling software perhaps if it has auto conversions to CSV or similar filing) to make it readable.

PS Using C++ instead of C wouldn't be a bad move too.
seems that somewhere you have typedef double REAL;

> 'fscanf_s': format string '%f' requires an argument of type 'float *', but variadic argument 1 has type 'double'
change the format flag to lf, and take the address of the variable you want to write (¿is this just one number?)
refi = fscanf_s( file1, "%lf \r\n", &narray);

now the second error, ¿don't you have syntax highlight? ¿don't you see `file1' in a weird colour?
fprintf_s( "file1, %f \r\n", nscal );
your quotes are wrong
fprintf_s( file1, "%f \r\n", nscal ); (also, note the %f flag and no memory address this time)


Edit: given that you are trying to modify the parameter in the first function, it shouldn't be passed by value
Last edited on
are you writing them as text? You may want more digits, eg %1.15f as the default is very low.
Topic archived. No new replies allowed.