I have a program that works when compiled in Windows with both Visual Studio and CodeBlocks, but when I compile it in Kubuntu using QT Creator, the fscanf functions don't work the same way.
I have a file containing the names of other files, each of which is separated by a space and line break. Using fscanf with "%s" in the working environments reads the file name into char entity[21]
which, in this test case, holds "ent001.txt" and ends in a null byte. In linux, however, entity[0] has "-74 / 182" then several null bytes, then several things that aren't in the file being read, none of them letters. Fscanf returns a -1.
Is there a deeper problem in portability, or are my standard libraries a bit off?
EDIT: For some sample code:
fin = fopen( levelfile, "r" ) ;
test = fscanf(fin, "%s", entity ) ;
Where 'levelfile' is 'char* levelfile[21]' whose value is hard coded in right now. Test is an 'int' to find the return value. 'fin' is not equal to null.
EDIT2: Output from xxd on the level file:
0000000: 656e 7430 3031 2e74 7874 200a 656e 7430 ent001.txt .ent0
0000010: 3032 2e74 7874 2024 200a 5472 6967 6765 02.txt $ .Trigge
0000020: 7230 3031 2开发者_如何学JAVAe74 7874 2024 200a 3020 3531 r001.txt $ .0 51
0000030: 3220 3531 3220 3020 0a31 2037 3132 2037 2 512 0 .1 712 7
Where did your data file get created? Any chance it has DOS-style line breaks (CR+LF) instead of Unix newlines?
If that's the problem, then text-mode (fopen(fname, "rt")
) may help or you can run the file through the dos2unix
utility (just d2u
on some Linus distributions).
精彩评论