sceIoOpen to create a text file ?

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

Moderators: cheriff, TyRaNiD

Post Reply
zoret
Posts: 22
Joined: Sun Mar 19, 2006 7:57 pm

sceIoOpen to create a text file ?

Post by zoret »

hi i have to create a text file but only can create a binary file
i use the following code, any idea ?
thanks alot !

Code: Select all

	char * pFileName = BuildFileName("tty", TEXT_EXT);
	int fd = sceIoOpen(pFileName, PSP_O_WRONLY/* | PSP_O_CREAT*/, 0777);
	
	sceIoLseek(fd, 0, SEEK_END);

	sceIoWrite(fd, "test1toto", 10);
	sceIoWrite(fd, "test2toto", 10);
	sceIoClose(fd);
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

Code: Select all

char szString[256] = "";
char* pFileName = BuildFileName("tty", TEXT_EXT); 
int fd = sceIoOpen(pFileName, PSP_O_WRONLY/* | PSP_O_CREAT*/, 0777); 
    
sceIoLseek(fd, 0, SEEK_END); 

sprintf( szString, "Hello there\n" );

sceIoWrite(fd, szString, strlen(szString) ); 
sceIoClose(fd);
zoret
Posts: 22
Joined: Sun Mar 19, 2006 7:57 pm

Post by zoret »

hey thanks but my code is ok
in the file i've the 2 strings
the problem is that the file contains the string in binary format

=> i see the strings in a soft like ultra edit, but not in note pad

this is similar to the "b" flag in the standard fopen function

is there another flag tha 0777 to specify that i open a text file and not a binary file ?

thanks !
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

actually theres not realy a difference between text and binary, maybe notepad doesnt recognize the file because of the zeros at the end of each string.
try this instead (not tested):

Code: Select all

char * pFileName = BuildFileName("tty", TEXT_EXT); 
   int fd = sceIoOpen(pFileName, PSP_O_WRONLY/* | PSP_O_CREAT*/, 0777); 
    
   sceIoLseek(fd, 0, SEEK_END); 

   sceIoWrite(fd, "test1toto\r\n", 11); 
   sceIoWrite(fd, "test2toto\r\n", 11); 
   sceIoClose(fd);
infj
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

zoret wrote:hey thanks but my code is ok
in the file i've the 2 strings
the problem is that the file contains the string in binary format

=> i see the strings in a soft like ultra edit, but not in note pad

this is similar to the "b" flag in the standard fopen function

is there another flag tha 0777 to specify that i open a text file and not a binary file ?

thanks !
Text is still binary. The only differences is sometimes in formating for things like '\n' will give you a 1 byte version vs a 2 byte version (the 1 byte version makes it look like there are no line breaks in notepad but yet when viewed in another tool it shows up with line breaks).

Looking over the code again. The bug at hand is because you are telling it to write 10 bytes when you are only passing it 9 bytes of data. So the function will add 1 line of junk data (usaly a 0). the null terminator character is breaking things when viewed in notepad (not sure why notepad does this).

I had this same issue before because I realised I added +1 to strlen when i did not need to (was afraid it would not fully write a new line command).

Truthfuly you're best bet is it go with something like what I posted or turn it into a function/macro.

#define WriteStrToFile( fid, str ) sceIoWrite( fid, str, strlen(str) )

WriteStrToFile( fd, "test1toto" );
zoret
Posts: 22
Joined: Sun Mar 19, 2006 7:57 pm

Post by zoret »

good idea.. that should work.. i have to test it !!
but i've another question, in all my tests the file was always created from scratch whereas i want to concatenate the string each time i boot my elf
that sounds strange since i seek to the end of the file before write it !

any idea ?
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

zoret wrote:good idea.. that should work.. i have to test it !!
but i've another question, in all my tests the file was always created from scratch whereas i want to concatenate the string each time i boot my elf
that sounds strange since i seek to the end of the file before write it !

any idea ?
not sure. i'll have to test that out in a bit.
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

zoret wrote:good idea.. that should work.. i have to test it !!
but i've another question, in all my tests the file was always created from scratch whereas i want to concatenate the string each time i boot my elf
that sounds strange since i seek to the end of the file before write it !

any idea ?
You need to add the PSP_O_APPEND flag (0x100).
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Post Reply