GP32 What's With This 26 Number?


dieu666

Still Fresh
Joined
Jan 22, 2004
Messages
29
i'm writing a program that write an unsigned char to a file which value is 26 to a file and then reading it and it doesn't work (although it's working with other numbers)

here's the code:

#include <stdio.h>

int main() {
FILE *fp;
unsigned char val1,val2;
val1 = 26;
printf("%d\n",val1);

fp = fopen("test","w");
fwrite(&val1,1,1,fp);
fclose(fp);

fp = fopen("test","r");
fread(&val2,1,1,fp);
fclose(fp);

printf("%d",val2);
getchar();
return 0;
}

that's a rather simple code so i dont know where i made a mistake, do you have any idea why it's not working with 26?

EDIT: with 26 the output is 0
 
when i say that doesn't work i mean the output is 0.

why the hell aren't you using ints?

cause MrMirko's SDK use unsigned char for sprite :D and the program i made convert header to bin but with the same structure as MrMirko's sprite + a few things that i need maybe what i'm doing is stupid but i still don't understand why it's not working with the number 26 and ONLY this one!
 
Are you building and running this program in DOS or Windows, by any chance? If so, your problem is probably because 26 is being treated as an end-of-file character. DOS defaults to text mode for files, which can cause all sorts of problems with non-text files. Open files with "wb" or "rb", for binary mode, if you want to store binary data in them.

Most operating systems don't make the (silly) distinction between text and binary files. I'm not sure what Mr. Mirko's SDK does.
Code:
#include <stdio.h>

int main() {
FILE *fp;
unsigned char val1,val2;
val1 = 26;
printf("%d\n",val1);

fp = fopen("test","wb");
fwrite(&val1,1,1,fp);
fclose(fp);

fp = fopen("test","rb");
fread(&val2,1,1,fp);
fclose(fp);

printf("%d",val2);
getchar();
return 0;
}
 
Back
Top