 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
darkshadow88
Joined: 09 Aug 2005 Posts: 2
|
Posted: Tue Aug 09, 2005 8:11 am Post subject: Problem with file input |
|
|
When I read from a file, and use pgPrint (from nem's hello world sample) to display the string, the string is displayed, but the rest of the screen is filled with checkerboard pattern boxes. The same char array displays fine when it is hard-coded into the program. I then used strlen to check the length of it and, for some reason, the string has a length of 777 instead of 5. How is this even possible, with the char array being defined with an upper bound of 256? What am I doing wrong?
| Code: | int main() {
/* SIZE is defined to be 256 earlier in the program. Changing this value
does not correct the problem */
char teststr[SIZE];
char teststr2[SIZE];
int fd;
int i;
//The file opened contains a 5-character string.
//I have also tried sceIoRead(fd, teststr, 5 * sizeof(char)); but it still did not work.
fd = sceIoOpen("ms0:/PSP/GAME/TEST/config", PSP_O_RDONLY, 0777);
sceIoRead(fd, teststr, 5);
sceIoClose(fd);
pgFillvram(0);
//Check and print length of string (for troubleshooting purposes)
i = strlen(teststr);
sprintf(teststr2, "%i", i);
pgPrint(0,29,0x7FFF,teststr2);
/*Print out string that was read in from file. This should just print the string, but it also fills the remainder of the screen with checkerboard-style characters (probably NULL characters, but I'm not sure) */
pgPrint(0,30,0x7FFF,teststr);
//Unrelated code here
} |
|
|
| Back to top |
|
 |
Snatcher
Joined: 19 Jul 2005 Posts: 9
|
Posted: Tue Aug 09, 2005 8:30 am Post subject: |
|
|
| You need to end each string with a NULL character, setting teststr[5] = '\0', since you are reading "raw" into the text array (and never initializing it, which is ok if you always validate) |
|
| Back to top |
|
 |
darkshadow88
Joined: 09 Aug 2005 Posts: 2
|
Posted: Tue Aug 09, 2005 8:39 am Post subject: |
|
|
Wow...that was awfully dumb of me *slams head on desk*
I guess that's what 14 hours of continuous programming does to you.
Thanks for your help :-) |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|