 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
mplacona
Joined: 07 Aug 2007 Posts: 28
|
Posted: Fri Aug 24, 2007 7:47 am Post subject: sceIo problems |
|
|
Hello guys,
Following up my last post here about copying an entire folder, I started to write my functions to do the job.
It compiles fine, but when I run it on the PSP, it freezes and shut down.
I'm trying to do a basic copy txt file and paste. The idea is to copy all the file contents and create a new text file with the contents.
It's very strange, 'cause basically it's kind of creating the files, but is corrupting the whole thing. And also, it is creating it as a binary. Is there any way to do it in a normal way, like "cloning" the file, and without corrupting it?
Here's my code. Is thre anything wrong there?
| Code: |
void copy(){
char write_buffer[128*1024];
//Opens the file
int target=sceIoOpen("ms0:/test.txt", PSP_O_WRONLY, 0777);
int newFile=sceIoOpen("ms0:/test1.txt", PSP_O_APPEND|PSP_O_CREAT, 0777);
//Creates a buffer for the file contents
char* data;
//Reads the contents
sceIoRead(target, &data, sizeof(data));
sprintf(write_buffer, data);
sceIoWrite(newFile, write_buffer, sizeof(write_buffer));
sceIoClose(target);
sceIoClose(newFile);
}
|
Cheers |
|
| Back to top |
|
 |
flatmush
Joined: 07 Aug 2007 Posts: 28 Location: Here
|
Posted: Fri Aug 24, 2007 8:24 am Post subject: |
|
|
OK, that is the most fudged code I can imagine. Why are you reading from a file you opened for writing, then appending it to another file. You are then attempting to read the data into an uninitialized array. Then you proceed to copy data for no reason using string functions which makes no sense. Also sizeof doesn't return the size of the array, it returns the size of the pointer which is 4 bytes.
What you are looking for is something like this:
| Code: | int fileCopy(char* inSrc, char* inDest) {
SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777);
if(!tempSrc)
return 0;
SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if(!tempDest) {
sceIoClose(tempSrc);
return 0;
}
char tempData[1024];
int tempLen = sceIoRead(tempSrc, tempData, 1024);
while(tempLen) {
sceIoWrite(tempDest, tempData, tempLen);
tempLen = sceIoRead(tempSrc, tempData, 1024);
}
sceIoClose(tempDest);
sceIoClose(tempSrc);
return 1;
}
|
I haven't tested that or checked it works, but I can't see any reason why it wouldn't. You will also notice that this streams the data across, so it should be able to copy files of any size. You could improve on the code further by making it recursive, and maybe using a funky for loop instead of that while. |
|
| Back to top |
|
 |
mplacona
Joined: 07 Aug 2007 Posts: 28
|
Posted: Fri Aug 24, 2007 9:33 pm Post subject: Yes |
|
|
Hi flatmush, you're right! The code was a bit messed up, and your code was great, it worked straight away. I just did some changes and it's fine now.
I was now wondering if it's possible to copy data from the PSP to a PC via USB. I know that the mass storage devices have some kind of limitations to do the job the other way round, but do you think I can do something in C to do that?
Thank you very much for your code, and soryy for mine [:(], but as I said, I'm still starting in the C development.
Cheers |
|
| Back to top |
|
 |
flatmush
Joined: 07 Aug 2007 Posts: 28 Location: Here
|
Posted: Fri Aug 24, 2007 10:11 pm Post subject: |
|
|
OK, that isn't my area of expertise, tyranid would be better suited to answer this question, but the usb driver is only programmed to act as an external storage device and the interface is on/off style I think. So basically you would have to write your own usb driver, and possibly write a driver for the pc if there isn't already one that does what you want.
It's probably not worth the effort.
Oh, and I decided to actually read the pspiofilemgr.h and I updated the filecopy slightly to make it a little better.
| Code: | int fileCopy(char* inSrc, char* inDest) {
SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777);
if(!tempSrc)
return -1;
SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if(!tempDest) {
sceIoClose(tempSrc);
return -2;
}
char tempData[1024];
int tempRead = sceIoRead(tempSrc, tempData, 1024);
int tempWritten;
while(tempRead > 0) {
tempWritten = sceIoWrite(tempDest, tempData, tempRead);
if(tempWritten != tempRead) {
sceIoClose(tempDest);
sceIoClose(tempSrc);
sceIoRemove(inDest);
return -3;
}
tempRead = sceIoRead(tempSrc, tempData, 1024);
}
sceIoClose(tempDest);
sceIoClose(tempSrc);
return 1;
}
|
|
|
| Back to top |
|
 |
Question_dev
Joined: 24 Aug 2007 Posts: 88
|
Posted: Mon Oct 29, 2007 3:00 am Post subject: |
|
|
is it also possible to copy dirs with it ?
| Code: | int fileCopy(char* inSrc, char* inDest) {
SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777);
if(!tempSrc)
return -1;
SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777);
if(!tempDest) {
sceIoClose(tempSrc);
return -2;
}
char tempData[1024];
int tempRead = sceIoRead(tempSrc, tempData, 1024);
int tempWritten;
while(tempRead > 0) {
tempWritten = sceIoWrite(tempDest, tempData, tempRead);
if(tempWritten != tempRead) {
sceIoClose(tempDest);
sceIoClose(tempSrc);
sceIoRemove(inDest);
return -3;
}
tempRead = sceIoRead(tempSrc, tempData, 1024);
}
sceIoClose(tempDest);
sceIoClose(tempSrc);
return 1;
} |
|
|
| Back to top |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Mon Oct 29, 2007 3:15 am Post subject: |
|
|
| Btw, I wouldn't place a 128*1024 array in the stack... |
|
| 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
|