Page 1 of 1

File writing / reading problem (SOLVED)

PostPosted: Sun Feb 03, 2013 1:42 pm
by lcl
Hello!

I'm having this little problem with file writing / reading..
I want to write a few pairs of integer values (x & y) indicating actor positions to file
and then load them and place actors to the right positions.

It works nearly perfectly. The only problem I keep getting is that the last actors position
isn't the one it should be, but instead it goes to the previous actors' position. :?

My writing code:
Code: Select all
int spawnPos[2];

void writeLevelFile(char * filePath)
{
    int i;
    FILE * WRITE = fopen(filePath, "w+b");
 
    for (i = 0; i < topIndex; i ++)
    {
        if (strcmp(getClone2("placeSpawn", i)->clonename, "") != 0)
        {
            spawnPos[0] = (int)round(getClone2("placeSpawn", i)->x - view.x);
            spawnPos[1] = (int)round(getClone2("placeSpawn", i)->y - view.y);
            fwrite(spawnPos, sizeof(spawnPos), 1, WRITE);
        }
    }
 
    fclose(WRITE);
}

And my reading code:
Code: Select all
int spawnPos[2];

void createSpawnPoints(char * filePath)
{
    int i;
    FILE * READ = fopen(filePath, "r+b");
 
    while (!feof(READ))
    {
        fread(spawnPos, sizeof(spawnPos), 1, READ);
        CreateActor("spawn", "spawn", "(none)", "(none)", spawnPos[0], spawnPos[1], false);
    }
 
    fclose(READ);
}

It probably is some really simple thing that I just can't spot, and that's why I need someone else to take a look at my code. :D
Thanks in advance!

Re: File writing / reading problem

PostPosted: Sun Feb 03, 2013 7:11 pm
by skydereign
The problem is probably your topIndex variable. You should make that for loop i<=topIndex since topIndex implies it is equal to the largest cloneindex.

Re: File writing / reading problem

PostPosted: Sun Feb 03, 2013 7:21 pm
by lcl
Yes, topIndex is equal to the largest cloneindex and I have tried using <=.
It works better than without the '=', but creates another problem.
Now all the positions are used, but two actors are created to the last position.

Thanks for replying anyway!

EDIT:

I managed to create a workaround.
It seems that the problem with '<=' was that the end of the file wasn't there immediately
after the last coordinates, so it created one more actor, but wasn't able to read any values to the
spawnPos[] array, so it used the old values. So, here's what I did:
Code: Select all
//I cut out the beginning of the code, as it's not changed
while (!feof(READ))
    {
        fread(spawnPos, 1, sizeof(spawnPos), READ);
        CreateActor("spawn", "spawn", "(none)", "(none)", spawnPos[0], spawnPos[1], false);

        fgetc(READ); //I had to advance the file position indicator by one character
        if (feof(READ))break; //Then check if the end of the file has been reached and exit the while-loop
        else fseek(READ, - 1, SEEK_CUR); //Or if the end was not reached, go back that one character
    }
//Also cut out the rest of the code

Kinda stupid workaround but gets the job done. :P

EDIT 2:

And thanks for helping. +1!

Re: File writing / reading problem

PostPosted: Sun Feb 03, 2013 7:31 pm
by skydereign
lcl wrote:Yes, topIndex is equal to the largest cloneindex and I have tried using <=.
It works better than without the '=', but creates another problem.

Well, you aren't saving everything if you don't use the <= (at least, I don't think you should be). Using it is the right way. If any problem exists after that, it is because of your read, which in this case is true. Essentially you are getting one extra failed read before it finds the eof. Easy way to fix it, is to check if fread actually read anything before creating (a good practice anyway).
Code: Select all
if(fread(spawnPos, sizeof(spawnPos), 1, READ))
{
    // CreateActor
}

Re: File writing / reading problem (SOLVED)

PostPosted: Sun Feb 03, 2013 7:45 pm
by lcl
skydereign wrote:
lcl wrote:Yes, topIndex is equal to the largest cloneindex and I have tried using <=.
It works better than without the '=', but creates another problem.

Well, you aren't saving everything if you don't use the <= (at least, I don't think you should be). Using it is the right way. If any problem exists after that, it is because of your read, which in this case is true. Essentially you are getting one extra failed read before it finds the eof.

Yeah, figured that out just after sending my previous message. This happens to me a lot. I just can't seem to find the logical solution and the reasons for errors alone. (Or then it requires me to take a break from what I'm doing.)
As you said, and now that I think of it, it actually is pretty obvious that it doesn't even write the information without the use of <=.
Anyway, that's a pretty nice way of checking if something was actually read. I should teach myself to use these kind of checks more to avoid errors like this.

Thanks for your time and help! :)