I am referring to VC++.
Hello,
I am experiencing a very strange bug when using
fscanf(). I have used it before and it always worked fine for me, but when I now try to read something from an opened file (
fopen() returns success!), it does
not always give me the expected results.
I have a
class C with
|-----a
char string[99].
|-----two integers:
int a,
int b.
Lets assume the file I want to read from is a text file with this content:
Quote:
loremipsumdolorsitamet 42 0
My reading code looks like this
FILE *stream = fopen( "file.txt", "r" ); //open file: returns success (line 1)
fscanf( stream, "%s", C->string ); //read in String (line 2)
fscanf( stream, "%d %d", &C->a, &C->b ); //read in Integers (line 3)
fclose( stream ); //close file. (line 4)
When I observe this part of the code during runtime,
C->string is undefined before line 2 and correctly containing "loremipsumdolorsitamet" after line 2. But when line 3 is executed,
C->string becomes "", or more exactly "\0\0\0emipsumdolorsitamet", which means the first 3 characters are kind of cut off!
But on the other hand the two integers are both assigned correctly. When I simply delete line 3, the string is also working. What makes me really confuse is that somewhere else in my program the same code works perfectly...
Does anybody have an idea what I am doing wrong?
rgds,
Phex