简体   繁体   中英

fscanf() behaves differently on different machines

The following line of code is supposed to read a string, an int and two doubles separated by commas and store them in variables.

fscanf(f,"%[^,],%d,%Lf,%Lf",name,&id,&east, &north);

It works on my Visual Studio 2010 compiler and reads the right values. I've run the program on another machine on which I've got random values for the three numbers but the right value for the string.

What could it be?

%Lf is for type long double , not double . On Microsoft compilers, long double has the same size and representation as double , so it happens to work, but your code is nonetheless invoking undefined behavior by using a mismatching format specifier. Use %lf with double , or change the type to long double if you want to use %Lf , and it should work everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM