forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sceIoOpen to create a text file ?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
zoret



Joined: 19 Mar 2006
Posts: 22

PostPosted: Wed Apr 19, 2006 8:54 am    Post subject: sceIoOpen to create a text file ? Reply with quote

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:
   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);
Back to top
View user's profile Send private message Send e-mail
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Thu Apr 20, 2006 1:10 am    Post subject: Reply with quote

Code:
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);
Back to top
View user's profile Send private message
zoret



Joined: 19 Mar 2006
Posts: 22

PostPosted: Thu Apr 20, 2006 3:57 am    Post subject: Reply with quote

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 !
Back to top
View user's profile Send private message Send e-mail
Saotome



Joined: 03 Apr 2004
Posts: 182

PostPosted: Thu Apr 20, 2006 6:53 am    Post subject: Reply with quote

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:

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
Back to top
View user's profile Send private message
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Fri Apr 21, 2006 5:56 am    Post subject: Reply with quote

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" );
Back to top
View user's profile Send private message
zoret



Joined: 19 Mar 2006
Posts: 22

PostPosted: Sat Apr 22, 2006 9:15 am    Post subject: Reply with quote

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 ?
Back to top
View user's profile Send private message Send e-mail
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Sat Apr 22, 2006 9:19 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Sat Apr 22, 2006 10:28 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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