Welcome! Log In Create A New Profile

Advanced

Saving and Loading Problem

Posted by Arikado 
Saving and Loading Problem
January 27, 2009 08:08PM
The following functions in WiiBreaker dont appear to be working right for me:


FILE *pattern;


void SavePattern(){
	
	pattern = fopen("fat:/apps/Wiibreaker/data/pattern1.pattern","rb");
	
	for(int i = 0; i < 30; i++){
	putc(brickx, pattern);
	putc(bricky, pattern);
	}
	
	fclose(pattern);
	
	}
	
	void LoadPattern(){
	
	pattern = fopen("fat:/apps/Wiibreaker/data/pattern1.pattern","rb");
	
	for(int i = 0; i < 30; i++){
	brickx = getc(pattern);
	bricky = getc(pattern);
	}
	
	fclose(pattern);
	
	}

Can anybody tell me what I'm doing wrong (and why)?

Thanks in advance for any and all help recieved. I truly appreciate it.

Arikado
Re: Saving and Loading Problem
January 27, 2009 08:23PM
What version of libfat are you using? If you're using the newest, it's sd:/ now, not fat:/. Another thing this is you should check the return value of fopen. When you're saving a file, you shouldn't be using "rb", use "wb" to open the file for writing.
Re: Saving and Loading Problem
January 27, 2009 08:28PM
It's always a good idea to check ALL return values. It'll help the users if something goes wrong, and you to know exactly what went wrong. That means checking the return value of putc and getc too. Also, although they're almost equivalent, you should use fputc and fgetc instead. putc and getc are meant more for macros.
Re: Saving and Loading Problem
January 27, 2009 08:56PM
Right now (before trying tantrics suggestions), my pattern1.pattern file is a blank 0 byte file. Do I need to put something on the file to allow it to save my variables?
Re: Saving and Loading Problem
January 27, 2009 08:59PM
What you should do is lookup the fopen function. It says:
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
Re: Saving and Loading Problem
January 28, 2009 01:25AM
Alright, so I've changed the code to the following:

FILE *pattern;

void SavePattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
	
	for(int i = 0; i < 30; i++){
	fputc(brickx, pattern);
	fputc(bricky, pattern);
	}
	
	fclose(pattern);
	
	}
	
	void LoadPattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
	
	for(int i = 0; i < 30; i++){
	brickx = fgetc(pattern);
	bricky = fgetc(pattern);
	}
	
	fclose(pattern);
	
	}
Unfortuanately, the loaded values are different that what they were when they were saved. Not that much different, but for what I'm making its kind of a problem.



Edited 4 time(s). Last edit at 01/28/2009 02:05PM by Arikado.
Re: Saving and Loading Problem
January 28, 2009 09:10AM
fputc(brickx, pattern); should be brickx ? In your save function.
Re: Saving and Loading Problem
January 28, 2009 02:03PM
Actually, it is brickx[ i ]. But for some reason, forum software is reading it as the italics tag. Notice how most of my code is in italics.

EDIT: Fixed my other post (mostly).



Edited 2 time(s). Last edit at 01/28/2009 02:06PM by Arikado.
Re: Saving and Loading Problem
January 28, 2009 02:29PM
I'm baffled. Any bricks with coordinates in the upperleft portion of the game window save and load fine. Any bricks saved with coordinates not in the upperleft portion of the game window are loaded with coordinates placing them in the upperleft portion of the game window.
Re: Saving and Loading Problem
January 28, 2009 02:50PM
what range of values do brickx and bricky have?

Writing char's to the file might be the problem as sizeof(char) is 1byte (range is: -128-127 or 0 to 255) .
Re: Saving and Loading Problem
January 28, 2009 02:54PM
BrickX Range: 0 - 640
BrickY Rage: 0 - 480

Thats actually a really interesting point Koopa. However, brickx and bricky are arrays of 30 ints so I'm pretty sure they can handle the range of numbers. If you could think of a better variable type though, I'd love to hear it.
Re: Saving and Loading Problem
January 28, 2009 02:54PM
hi Arikado,

just a wild guess.
maybe somehow there is an overflow.
putting an int16 in a int8 somewhere?

int8 i=257 ;
would result in
i=2;

i guess coordinates (0,0) would be upperleft ?
hope this helps.

greetingz,
Peter de Man
Re: Saving and Loading Problem
January 28, 2009 03:03PM
Using int for the coordinates is fine.

You'll just want to use fwrite(..) instead of fputc for writing data and fread(...) instead of fgetc(..) for reading data.
Re: Saving and Loading Problem
January 28, 2009 03:19PM
Forgive me if I'm wrong but am I missing something here?

You say the values you wish to use are 0 to 640 and 0 to 480 but doesn't fputc just write 1 character and fgetc just reads 1 character?

Edit: I agree with koopa.



Edited 2 time(s). Last edit at 01/28/2009 03:23PM by teknecal.
Re: Saving and Loading Problem
January 28, 2009 03:24PM
hi,

i was typing at the same time you did so i didn´t knew (else i wouldn´t have posted).
maybe arikado was getting a lead from my post.
but koopa´s makes even more sence :D

i dont know anything about Wii programming just programming in general.
but now basicly i understand:
the using Char (8 bits ) as parsing medium/function is corrupting this Int ( > 8 bits)

just curious.

warm greetingz,
Peter de Man



Edited 1 time(s). Last edit at 01/28/2009 03:26PM by PeterdeMan.
Re: Saving and Loading Problem
January 28, 2009 03:55PM
What are the parameters for fwrite and fread?
Re: Saving and Loading Problem
January 28, 2009 04:22PM
Re: Saving and Loading Problem
January 28, 2009 08:57PM
Thanks. I changed my functions accordingly, but I appear to have broken loading. There are probably alot of stupid errors in it:

void SavePattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
	
	for(int i = 0; i < 30; i++){
	fwrite(brickx, 1, sizeof(brickx), pattern);
	fwrite(bricky, 1, sizeof(bricky), pattern);
	}
	
	fclose(pattern);
	
	}
	
	void LoadPattern(){
	
	long LSize;
	char * buffer;
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
	
	// obtain file size:
    fseek (pattern , 0 , SEEK_END);
    LSize = ftell (pattern);
    rewind (pattern);
	buffer = (char*) malloc (sizeof(char)*LSize);

	for(int i = 0; i < 30; i++){
	brickx = fread(buffer, 1, LSize, pattern);
	bricky = fread(buffer, 1, LSize, pattern);
	}
	
	fclose(pattern);
	free(buffer);
	
	}
Re: Saving and Loading Problem
January 28, 2009 09:24PM
Ok, great. That's looking better. But, still some mistakes.

Why don't you just do this instead:

in Save function:

fwrite(brickx, 1, sizeof(brickx)*30, pattern);
fwrite(bricky, 1, sizeof(bricky)*30, pattern);

in Load function:

fread(brickx, 1, sizeof(brickx)*30, pattern);
fread(bricky, 1, sizeof(bricky)*30, pattern);

Keep in mind the definitions of functions. fread returns the NUMBER of bytes read, not the VALUE.

This line reads in the whole file, and stores the # of bytes in the file in brickx (hopefully LSize)
brickx = fread(buffer, 1, LSize, pattern);

As for error-checking, perhaps check when you get the filesize that LSize == sizeof(brickx)*30 + sizeof(bricky)*30
Otherwise it's a bad file.
Re: Saving and Loading Problem
January 29, 2009 10:48AM
When sizeof is applied to an array, the result is the size in bytes of the array in memory. The third parameter to fwrite is not the size in bytes of the array but the number of elements in the array.

so
fwrite(brickx, sizeof(int), 30, pattern);


will write the complete array brickx to your file. You don't need a loop for that.

fwrite(&brickx, sizeof(int), 1, pattern);
will write one element of the array brickx to the file. You'll want to use the for loop in this case.

same for loading:

...
int brickx[30];
size_t elementsRead = fread(brickx, sizeof(int), 30, pattern);
...

reads the complete array from the file.

resp.

...
int brickx[30];
int bricky[30];

for (int i = 0; i < 30; i++){
  //read one element at a time
  fread(&brickx, sizeof(int), 1, pattern);
  fread(&bricky, sizeof(int), 1, pattern);
....
}
...

Cheers,

Koopa
Sorry, only registered users may post in this forum.

Click here to login