| View previous topic :: View next topic |
| Author |
Message |
zoret
Joined: 19 Mar 2006 Posts: 22
|
Posted: Wed Apr 19, 2006 8:54 am Post subject: sceIoOpen to create a text file ? |
|
|
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 |
|
 |
subbie
Joined: 05 May 2005 Posts: 122
|
Posted: Thu Apr 20, 2006 1:10 am Post subject: |
|
|
| 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 |
|
 |
zoret
Joined: 19 Mar 2006 Posts: 22
|
Posted: Thu Apr 20, 2006 3:57 am Post subject: |
|
|
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 |
|
 |
Saotome

Joined: 03 Apr 2004 Posts: 182
|
Posted: Thu Apr 20, 2006 6:53 am Post subject: |
|
|
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 |
|
 |
subbie
Joined: 05 May 2005 Posts: 122
|
Posted: Fri Apr 21, 2006 5:56 am Post subject: |
|
|
| 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 |
|
 |
zoret
Joined: 19 Mar 2006 Posts: 22
|
Posted: Sat Apr 22, 2006 9:15 am Post subject: |
|
|
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 |
|
 |
subbie
Joined: 05 May 2005 Posts: 122
|
Posted: Sat Apr 22, 2006 9:19 am Post subject: |
|
|
| 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 |
|
 |
Fanjita
Joined: 28 Sep 2005 Posts: 217
|
Posted: Sat Apr 22, 2006 10:28 am Post subject: |
|
|
| 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 |
|
 |
|