SDL File Position Problems

Started by
3 comments, last by TotusTuus07 16 years, 2 months ago
I am rewriting a piece of scripting code that was written in Windows (using mostly DirectX stuff) for Linux (using SDL). I had an error that was plaguing me earlier - everything worked fine until I declared a character string in the constructor of a derived class - it caused a completely unrelated base class function to fail (it involved setting a colorkey). I was getting a SIGSEGV error and it took me awhile to figure out what was going on. Eventually I came to the conclusion that there was a problem with the scripting system. You see, when porting over this (perfectly working) code from Visual Studio to KDevelop, I had to change an area of code: fscanf( file, "%c", buffer ); while( true ) { if( strcmp( buffer, "\"" ) == 0 ) { commasFound = true; break; } if( strcmp( buffer, " " ) != 0 ) { fpos_t pos; fgetpos( file, &pos ); fsetpos( file, &--pos ); break; } fscanf( file, "%c", buffer ); } The part where I used fsetpos with the &--pos operator gave me an error. It said something like the -- operator wasn't defined with an fpos_t variable. So I changed it to something that obviously didn't work. I was curious as to whether anyone could give me a good replacement for that line... fsetpos( file, &--pos ); ... that is platform independent? Thanks in advance.
Advertisement
Why not just do fseek( file, -1, SEEK_CUR )?
Looking further at this code, there are a lot of problems here. You're continually reading characters into buffer, but assuming buffer is a character array, then you're just writing the first character in the array over and over again. Also, you read a single character into the first position in the array with %c, but %c doesn't set the next character to nul. Just after that you use strcmp, which if the second character in the array is not nul will always fail.

It looks like what you need is a real tokenizer.
Well, this is just the portion that reads the first character of the token. The program already knows that the token is a string, and it has it's name... it just needs to get the value. So this loop continues until it either gets a quote or a non space character.

This is the part that comes after the program knows it is a string. What it does is get the next character:

fscanf( file, "%c", buffer );

Then it loops.

It compares the character to a quote. If it finds it, it notes it (commasFound) and breaks the while loop... Then it knows the next character is the first character of the string.

Otherwise it tries to see if the character is not a space. If it is not a space, then it backs up the file position (because it already read in whatever this character was) and breaks the loop.

If the character is a space, it gets the next character and loops.

I guess it may be hard to tell because the code is not indented.

I will try the fseek. I had it typed in as fseek( file, 0, SEEK_CUR ); for some strange reason.
I figured it out... and you were right. I had to rewrite the tokenizer. I got it out of a book, and it worked in Windows, so I assumed that since it worked at first it was okay. After editing a few portions and debugging, I got the results I wanted.

Thanks for pointing me in the right direction.

This topic is closed to new replies.

Advertisement