Problem with file input

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
darkshadow88
Posts: 2
Joined: Tue Aug 09, 2005 8:02 am

Problem with file input

Post by darkshadow88 »

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: Select all

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
}
Snatcher
Posts: 9
Joined: Tue Jul 19, 2005 2:31 am

Post by Snatcher »

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)
darkshadow88
Posts: 2
Joined: Tue Aug 09, 2005 8:02 am

Post by darkshadow88 »

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 :-)
Post Reply