File I/O Woes

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

Moderators: cheriff, TyRaNiD

Post Reply
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

File I/O Woes

Post by Mak0 »

ok let me start off by saying I still consider myself very much so a newbie when it comes to coding in C, especially with the unintuitiveness of the gcc compiler. I've had pretty good success using nem's pg library so far as I've got some simple "hello world" type programs to compile and run successfully. The problem I'm having is with the psp's file I/O functions. I have modified startup.s correctly and it even compiles 100% fine with no errors or warnings, but when I run it file2.txt is not even created. All I'm trying to accomplish is a mere copying of one file on the memory card. Here's my code (as you can probably tell it's based on nem's hello world):

Code: Select all

#include "pg.h"

int xmain(void)
{
	unsigned long fc;
	unsigned long r,g,b,rgb;

	#define O_RDONLY	0x0001
	#define O_WRONLY	0x0002
	#define O_RDWR		0x0003
	#define O_NBLOCK	0x0010
	#define O_APPEND	0x0100
	#define O_CREAT		0x0200
	#define O_TRUNC		0x0400
	#define O_NOWAIT	0x8000
	
	pgInit();
	pgScreenFrame(2,0);
	pgFillvram(0);
	
	int inF, ouF;
        char line[512];
        int bytes;

 if((inF = sceIoOpen("ms0:/file1.txt", O_RDONLY)) == -1) {
    
    return(1);
  }

  if((ouF = sceIoOpen("ms0:/file2.txt", O_WRONLY | O_CREAT)) == -1) {
   
    return(1);
  }
while((bytes = sceIoRead(inF, line, sizeof(line))) > 0)
    sceIoWrite(ouF, line, bytes);

  sceIoClose(inF);
  sceIoClose(ouF);

	fc=0;
	while (1) {
		fc++;
		if (fc>=1536) fc=0;
		
		if &#40;fc<256&#41; &#123;
			r=255;		g=0;		b=fc;
		&#125; else if &#40;fc<512&#41; &#123;
			r=511-fc;	g=0;		b=255;
		&#125; else if &#40;fc<768&#41; &#123;
			r=0;		g=fc-512;	b=255;
		&#125; else if &#40;fc<1024&#41; &#123;
			r=0;		g=255;		b=1023-fc;
		&#125; else if &#40;fc<1280&#41; &#123;
			r=fc-1024;	g=255;		b=0;
		&#125; else &#123;
			r=255;		g=1535-fc;	b=0;
		&#125;
		r=r/8;
		g=g/8;
		b=b/8;
		rgb=&#40;b<<10&#41;+&#40;g<<5&#41;+&#40;r<<0&#41;+0x8000;

		pgPrint2&#40;4,3,rgb,"Please Work!,"&#41;;
		

		pgScreenFlipV&#40;&#41;;
	&#125;

	return 0;
&#125;
This is really starting to bug me. Any help is immensely appreciated!
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

First of all, do you see your "please work" show up at all? If not, what I would do to help debug is make sure that you can get output at the end of your program, then set flags in each of your if statements to make sure that you are getting into them. Set a flag making sure you are opening "file1.txt" and opening "file2.txt". Then set a flag saying that you are going into your while loop.
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

yes, "please work" displays fine and it changes colors just like its supposed to.
0xdeadface
Posts: 62
Joined: Tue May 31, 2005 5:11 am

Post by 0xdeadface »

Why test for -1 with the open?

There are many error codes possible I think, so I would at least test for < 0 and see if it still gets to the printf.

0xdf
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

tried it with < 0, it still gets to the printf and still doesnt even create the file :(
0xdeadface
Posts: 62
Joined: Tue May 31, 2005 5:11 am

Post by 0xdeadface »

A guess, does "sceIoOpen("ms0:/file2.txt", O_WRONLY | O_CREAT, 0777)" improve anything?

0xdf
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

still nothing :(

would anyone happen to have a file copy code snippet they've used in a project or something that worked that I could look at?
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

weak does it like this in his screenshot code:
file = sceIoOpen(savePath,O_CREAT | O_TRUNC | O_RDWR, 0777);
infj
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

Saotome wrote:weak does it like this in his screenshot code:
file = sceIoOpen(savePath,O_CREAT | O_TRUNC | O_RDWR, 0777);
It worked!

thanks! :)
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

just one more quick question:

do I have to do anything special to access umd0?

thanks again for all you guys' help
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Mak0 wrote:just one more quick question:

do I have to do anything special to access umd0?

thanks again for all you guys' help
Yes, but answering this question is not allowed in this forum. Do some disassembling of some maybe illegal program, if you want to know more.
Mak0
Posts: 36
Joined: Thu Jan 27, 2005 8:56 am

Post by Mak0 »

Are there certain things I can and cannot ask about the UMD device or is anything at all dealing with it off limits?

The reason I ask is that I simply want to be able to access individual files on my legally purchased retail games without having to dump entire images of them.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

Mak0 wrote:Are there certain things I can and cannot ask about the UMD device or is anything at all dealing with it off limits?

The reason I ask is that I simply want to be able to access individual files on my legally purchased retail games without having to dump entire images of them.
Accessing individual files could be used for dumping games (because there are functions to list the directory) and The Rules Of The PSP Forums says "Development of methods to dump your own games is off limits", so you have to ask elsewhere.
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

Anybody figure out why its required to use the "O_TRUNC" flag when creating a file? This is something I have noticed from development. If you dont use that flag it will fail on trying to creat the file if it does not exsist.
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

Hi, i think its the good topic to post my question.

I 'm trying to deal with the sceiowrite and sceioread functions, i have succes to use the write function, i save a selected path to a file called temp.dat, and i want to define a TARGETPATH with the path i successfully writed to the temp.dat file.

Originaly i have to put in my code :
#define TARGETPATH "ms0:/PSP/GAME/TEMP/TEMP.BIN"
I want this define path to be the one in my temp.dat file and i dont know how, i tested a lot of possibility :/

Here is the good sceiowrite code :
if (key & CTRL_START) {
int fd;
fd = sceIoOpen("ms0:/PSP/temp.dat",O_CREAT|O_WRONLY|O_TRUNC, 0777);
sceIoWrite(fd,target, 90);
sceIoClose(fd);
}
So now if u understand my poor english, i want to define a path with the one in the temp.dat file to use it later ( eg: fd2 = sceIoOpen(TARGETPATH,O_WRONLY | O_CREAT | O_TRUNC, 0777);)

Here is the very bad code i used to define the TARGETPATH variable (it would be to easy if it worked) :
//void GetTARGETPATH(int)
{
int fd;
int bytes1;

fd = sceIoOpen("ms0:/PSP/pspf.dat",O_RDONLY, 0777);
bytes1 = sceIoRead(fd, &tpath, 90);
sceIoClose(fd);
TARGETPATH = tpath
}
Again sorry for my poor english ... and my poor skill.
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

using #define is just a macro used by the CPP (C PreProcessor) that says "every time you see THIS which was defined, replace it with what goes after it". So in your code, you have:

Code: Select all

#define TARGETPATH "ms0&#58;/PSP/GAME/TEMP/TEMP.BIN"
and then a

Code: Select all

TARGETPATH = tpath
so when it compiles, what you end up with is:

Code: Select all

"ms0&#58;/PSP/GAME/TEMP/TEMP.BIN" = tpath
So what I say is just make TARGETPATH a variable instead of a #define.
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

hehe yes its what i'm trying to do, and its the question, what is the code to define this as a variable including the sceioread function :/
ector
Posts: 195
Joined: Thu May 12, 2005 10:22 pm

Post by ector »

something like

char name[256];
blah blah open fd
sceIoRead(fd, name, 255);
blah blah close fd

then use name as the file to read
sceIoOpen(name, ....)

But you should really read up on C before trying to code on something as foreign as the PSP :)
Cpasjuste
Posts: 214
Joined: Sun May 29, 2005 8:28 am

Post by Cpasjuste »

oK many thanks, i figured out how it work. Your right i need to improve my c skill, i start learn it with some simple tutorials, but its not really easy without any help hehe.
Post Reply