Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
March 11, 2010, 08:39:05 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Join our IRC channel: ##psp-programming on freenode
Home Help Search Shop Login Register
Digg This!
Poll
Question: Was this helpful?  (Voting closed: May 07, 2006, 07:31:42 AM)
Yes - 21 (91.3%)
No - 2 (8.7%)
Total Voters: 23

Pages: [1] 2
Print
Author Topic: PSP File I/O Functions  (Read 16434 times)
Feldor
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 47
0.00 points

View Inventory
Send Money to Feldor


View Profile
« on: May 07, 2006, 07:31:42 AM »

Alright, From what ive learned through my experiences with file input and output with the psp I am going to write a small guide on the commands and include examples:

All of these functions need this include to be added with the rest of the included headers.

Code:
#include <pspiofilemgr.h>


1. sceIoOpen()
    This command opens a file for reading/writing/whatever, It returns the File ID when used.

SceUID sceIoOpen(const char *file, int flags, SceMode mode);

const char *file
This is the filename including its path (/blah/blah/test.txt)

int flags
This part of the command is where you define what your going to do with the file, and you may use these flags as defined in pspiofilemgr_fcntl.h:

PSP_O_RDONLY - Only opens file for reading
PSP_O_WRONLY - Only opens file for writing
PSP_O_RDWR - Does both reading and writing
PSP_O_NBLOCK - Not quite sure what this is, but you dont have to worry about it
PSP_O_DIROPEN - This is an internal directory flag dont worry about it
PSP_O_APPEND - Opens the file and goes to the end of the file
PSP_O_CREAT - Creates the file if it doesnt exists
PSP_O_TRUNC - Truncates the file when opened
PSP_O_EXCL - Also not sure of its purpose
PSP_O_NOWAIT - Not sure, but assuming it has to do with not waiting for something opening the file mabye? not sure. Wink

SceMode mode
This im not quite sure of what it represents, the mode, mabye file number, im not quite sure, but in all the examples and include files, 0777 is used, so its safe to say to use 0777 for this parameter.

    Example

Code:
char fpath[200];
int fd;

// This sets the file to be opened to be whatever 'file' is
// This file is located in the same place as the EBOOT.PBP
sprintf(fpath, "%s.txt", file);

// Check if we can open the file
if(!(fd = sceIoOpen(fpath, PSP_O_RDONLY, 0777))) {
   // Error if we cant open it
} else {
   // Continue if we can open it
}

//  Must make sure to close the file I/O before continuing with the rest of the program
sceIoClose(fd);


[/list]

2. sceIoClose()
    This command is used to close the file that has been opened, it returns a number less than 0 if theres an error.

int sceIoClose(SceUID fd);

SceUID fd
This is the file ID, to the file that has been opened with sceIoOpen()

Example

Code:
int fd;

// opens the file for reading only
fd = sceIoOpen(fpath, PSP_O_RDONLY, 0777);

// insert reading code here

// closes the file
sceIoClose(fd);
[/list]

3. sceIoRead()
    This command is used to read the contents of the file, it also returns the number of bytes that it reads.

int sceIoRead(SceUID fd, void *data, SceSize size);

SceUID fd
This is the files ID

void *data
This is where the data that is read is put

SceSize size
This is where you can define how many bytes you want to read.

Example
Code:
char fpath[200];
char read_buffer[128*1024];

int fd;
int flen;

sprintf(fpath, "%s.txt", file);

if(!(fd = sceIoOpen(fpath, PSP_O_RDONLY, 0777))) {
   // Error if we cant open it
} else {
   flen = sceIoRead(fd, read_buffer, sizeof(read_buffer));
   if(flen <= 0) {
      // if flen is 0 or lower, this means that the file is empty, it didnt read any bytes.
   }
}

//  Must make sure to close the file I/O before continuing with the rest of the program
sceIoClose(fd);


[/list]

4. sceIoWrite()
    This commands used to write into the file that has been opened, returning the number of bytes it writes.

int sceIoWrite(SceUID fd, const void *data, SceSize size);

SceUID fd
This is the files ID

const void *data
This is the data that is written into the file

SceSize size
This is the size of the data that is to be written into the file

Example

Code:
char fpath[200];
char write_buffer[128*1024];

int fd;
int flen;

sprintf(fpath, "%s.txt", file);

// Open the file for writing only and append it to the end of the file, create the file if it doesnt exist.
if(!(fd = sceIoOpen(fpath, PSP_O_WRONLY|PSP_O_APPEND|PSP_O_CREAT, 0777))) {
   // Error if we cant open it
} else {
   sprintf(write_buffer, "Horay Feldor is the best! bwhahahahahahaa!!");

   // Writes write_buffer to the file that we opened.
   flen = sceIoWrite(fd, write_buffer, sizeof(write_buffer));
   if(flen <= 0) {
      // iif flen is <= 0 then no bytes were written to the file, and this is a bad thing.
   }

}

//  Must make sure to close the file I/O before continuing with the rest of the program
sceIoClose(fd);

[/list]

Thats all for now, ill add more complex examples later to show more functionality of these commands.

** If ive made any mistakes or forgotten something important please let me know to improve this guide **
Logged


All my work is licensed under the Creative Commons (CC) License.


TeamOverload
Jr. Member
**

Karma: +0/-0
Offline Offline

Posts: 95
0.00 points

View Inventory
Send Money to TeamOverload

View Profile WWW
« Reply #1 on: May 07, 2006, 07:37:15 AM »

Nice guide once the sdk decides to actually setup right, this should help
Logged

Nothing is unhackable!
Mianvro
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 21
0.00 points

View Inventory
Send Money to Mianvro


View Profile WWW
« Reply #2 on: May 07, 2006, 07:38:43 AM »

The 'mode' looks like a CHMOD flag.
If that's what it is, you should be able to bind files to users.

Have you tried writing a file with mode 0744 in kernel mode, and then try to write to it in user mode?

Note: I could be way off  Embarassed


Edit: Some errors corrected  Cool
Logged

Watch me!
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #3 on: May 07, 2006, 07:40:11 AM »

Great guide Very Happy

Would you mind if I put it into tutorial forum and put it up with the rest of the tutorials?
Logged

Feldor
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 47
0.00 points

View Inventory
Send Money to Feldor


View Profile
« Reply #4 on: May 07, 2006, 07:46:29 AM »

Quote from: "Mianvro"
The 'mode' looks like a CHMOD flag.
If that's what it is, you should be able to bind files to users.

Have you tried writing a file with mode 0744 in kernel mode, and then try to write to it in user mode?

Note: I could be way off  Embarassed


Edit: Some errors corrected  Cool


Yea that seems to be quite possible now that I think of it, but i wont be able to try it in kernel mode, I use fw2.6 Smile

Is anyone else able to confirm this?

Quote from: "Yeldarb"

Great guide Very Happy

Would you mind if I put it into tutorial forum and put it up with the rest of the tutorials?



Thanks Smile, and yes sure you can put it there but will i but will i be able to add to it? change it if the mode actually is a CHMOD flag?
Logged


All my work is licensed under the Creative Commons (CC) License.
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #5 on: May 07, 2006, 07:55:45 AM »

I'll see what I can do, not sure how I'd set that up.

If it ends up that I'm not able to get that set up, you can always just PM/email me to fix/update it.
Logged

Mianvro
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 21
0.00 points

View Inventory
Send Money to Mianvro


View Profile WWW
« Reply #6 on: May 07, 2006, 07:57:29 AM »

I'm installing the toolchain on my Debian machine this very moment.

The basic idea would be:
Program 1:
Access kernel mode, write file with 0744 flags.

Program 2:
Access user mode, try to write file written by program 1 and see if it works.

Couldn't be that difficult, though I haven't worked with any of the I/O functions yet.
So I'll be putting your tutorial to the test too  Razz
Logged

Watch me!
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #7 on: May 07, 2006, 07:58:51 AM »

Quote from: "Mianvro"
I'm installing the toolchain on my Debian machine this very moment.

The basic idea would be:
Program 1:
Access kernel mode, write file with 0744 flags.

Program 2:
Access user mode, try to write file written by program 1 and see if it works.

Couldn't be that difficult, though I haven't worked with any of the I/O functions yet.
So I'll be putting your tutorial to the test too  Razz

Great, let us know how it turns out Very Happy
Logged

Feldor
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 47
0.00 points

View Inventory
Send Money to Feldor


View Profile
« Reply #8 on: May 07, 2006, 07:59:21 AM »

Quote from: "Yeldarb"
I'll see what I can do, not sure how I'd set that up.

If it ends up that I'm not able to get that set up, you can always just PM/email me to fix/update it.


Alright yea that would be fine too.

@ Mianvro : Lol i hope i pass the test Very Happy and keep me posted.
Logged


All my work is licensed under the Creative Commons (CC) License.
nathan42100
Give miinaturvat Points!
Administrator
Hero Member
*

Karma: +31/-2
Offline Offline

Posts: 1158
804.49 points

View Inventory
Send Money to nathan42100


View Profile WWW
« Reply #9 on: May 07, 2006, 09:06:31 AM »

I'm pretty sure you could also used the default C File I/O functions. I changed the title to specify that it is file i/o, not gamepad i/o.
Logged

All of my work is released under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.
dn ʎɐʍ sıɥʇ
Help support my computer projects!

Mianvro
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 21
0.00 points

View Inventory
Send Money to Mianvro


View Profile WWW
« Reply #10 on: May 07, 2006, 09:18:45 AM »

I'm having a little delay here.
Can't seem to get my PSP working on Linux yet Sad

Now I got to use Cygwin, not sure if it is gonna run after my windows install Razz
I'll keep you posted Smile
Logged

Watch me!
Mianvro
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 21
0.00 points

View Inventory
Send Money to Mianvro


View Profile WWW
« Reply #11 on: May 07, 2006, 11:21:08 AM »

Alright guys!

Either I was wrong or the PSP is too stupid to understand permissions  Wink


I've created a file "test.txt" in "ms0:/", and wrote a line to it in kernel mode.
Permissions 0744 so read only for anyone but owner.

Then, in user mode, I tried to write a line to the file, no problems occurred.

Conclusion:
We still don't know what "mode" does  Laughing

Source and binaries
Logged

Watch me!
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #12 on: May 07, 2006, 12:14:42 PM »

It could just be left over from the compiler they started out with (they didnt make it from scratch, I think they used GCC)
Logged

jsharrad
Give Miinaturvat points!
Global Moderator
Hero Member
*

Karma: +41/-1
Offline Offline

Posts: 603
764.52 points

View Inventory
Send Money to jsharrad

Yarr!


View Profile
« Reply #13 on: May 07, 2006, 12:31:24 PM »

Yeldarb has it I believe, it is left over from a *nix environment where permissions are set on the file when its created.  Lets the OS which usergroups can read, write, and execute the file, I don't believe it has anything to do with the PSP.  0777 mask means everyone can use it anyway.
Logged

MinerPSP Coder
MinerPSP Website
Mianvro
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 21
0.00 points

View Inventory
Send Money to Mianvro


View Profile WWW
« Reply #14 on: May 07, 2006, 12:36:59 PM »

Quote from: "jsharrad"
0777 mask means everyone can use it anyway.

That's why I tried 0744, to test if the PSP would do anything with it.
And by using different users ( kernel & user mode ) I found out it doesn't.
User mode shouldn't have been able to edit the file made by kernel mode since it is not the owner and should only be able to read.

So, actually, the 'mode' is quite useless.
Logged

Watch me!
Pages: [1] 2
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.296 seconds with 38 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com