Thread: File I/O - working incorrectly

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    File I/O - working incorrectly

    I'm working on level saving, in the engine i've been working on.
    I've created the following functions:

    save:
    Code:
    void level::savestaticmesh(char *fname)
    {
    	remove(fname);
    
    	for (int i=0; i<num_meshs; i++)
    	{
    		stream = fopen(fname, "a");
    		fprintf(stream, "%s %s %s %d %d %d %f %f %f %f %f %f %f %f %f\n", 
    			meshP[i].filename, meshP[i].tex1, meshP[i].tex2, meshP[i].textype, meshP[i].repeatx, meshP[i].repeaty, 
    			meshP[i].pos.x, meshP[i].pos.y, meshP[i].pos.z, 
    			meshP[i].scale.x, meshP[i].scale.z, meshP[i].scale.y,
    			meshP[i].rot.x, meshP[i].rot.y, meshP[i].rot.z);
    		fclose(stream);
    	}
    }
    load:
    Code:
    void level::loadstaticmesh(char *fname)
    {
    	stream = fopen(fname, "r");
    
    	while(1)
    	{
    		int result = fscanf(stream, "%s %s %s %d %d %d %f %f %f %f %f %f %f %f %f\n", 
    			tmpfname, tmptex0, tmptex1, &tmptype, &tmprx, &tmpry, 
    			&tmppos.x, &tmppos.y, &tmppos.z, 
    			&tmpscale.x, &tmpscale.z, &tmpscale.y,
    			&tmprot.x, &tmprot.y, &tmprot.z);
    
    		if(result==EOF)
    			break;
    
    		meshnum++;
    	}
    
    	rewind(stream);
    
    	for(int i=0; i<meshnum; i++)
    	{
    		fscanf(stream, "%s %s %s %d %d %d %f %f %f %f %f %f %f %f %f\n", 
    			tmpfname, tmptex0, tmptex1, &tmptype, &tmprx, &tmpry,
    			&tmppos.x, &tmppos.y, &tmppos.z, 
    			&tmpscale.x, &tmpscale.z, &tmpscale.y,
    			&tmprot.x, &tmprot.y, &tmprot.z);
    		if(tmprx==0)
    			tmprx=1;
    		if(tmpry==0)
    			tmpry=1;
    		_ceStaticMesh->createstaticmesh(tmpfname, tmptex0, tmptex1, tmptype, tmprx, tmpry,
    			tmppos.x, tmppos.y, tmppos.z,
    			tmprot.x, tmprot.y, tmprot.z,
    			tmpscale.x, tmpscale.y, tmpscale.z);
    	}
    	fclose(stream);
    }
    I've run into some trouble though.
    Suppose I create two cubes. They each have different texture names, and different positions. I click the 'save' button, and exit the engine. Restart the engine, both cubes are still rendered with different textures; the save appears to have worked correctly. Click 'save' and exit again. Restart, both cubes now have the same texture; the second save messed up.

    Just for clarification, the load function reads through the formatted text, and creates a staticmesh entity with the createstaticmesh() function. The function arguments are stored in the array meshP[]. On save, the meshP[] array index is cycled through, and the arguments are read into the file...I'm sure you get the idea.

    Any help will be very much appreciated!
    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Do any of the char arrays you write have spaces? If they do, this could cause problems when you read the file.

    You should probably check to make sure you actually read 15 items:
    Code:
    	while (fgets(buf, sizeof(buf), fp) != NULL)
    	{
    		int result = sscanf(buf, "%s %s %s %d %d %d %f %f %f %f %f %f %f %f %f\n", 
    			tmpfname, tmptex0, tmptex1, &tmptype, &tmprx, &tmpry, 
    			&tmppos.x, &tmppos.y, &tmppos.z, 
    			&tmpscale.x, &tmpscale.z, &tmpscale.y,
    			&tmprot.x, &tmprot.y, &tmprot.z);
    		if (result != 15)
    			printf("Error reading file.\n");
    	}

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    why 15?

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >why 15?
    fscanf and sscanf both return how many items were read. It looks like you are trying to read 15 items.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Ah, gotcha. I though you meant 15 loop cycles
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Ah, gotcha. I though you meant 15 loop cycles
    I guess what it is I'm getting at, since you said the second save messed up, how many records did it actually write, and were you able to read each line of data back.

    It kind of has the feel like one of the reads may have only read a few items, not all 15.

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Ran it through the debugger, and everything is reading back, but the texture name values change for some reason??
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM