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.
#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 *fileThis is the filename including its path (/blah/blah/test.txt)
int flagsThis 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.
SceMode modeThis 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.
Examplechar 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 fdThis is the file ID, to the file that has been opened with sceIoOpen()
Exampleint 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 fdThis is the files ID
void *dataThis is where the data that is read is put
SceSize sizeThis is where you can define how many bytes you want to read.
Examplechar 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 fdThis is the files ID
const void *dataThis is the data that is written into the file
SceSize sizeThis is the size of the data that is to be written into the file
Examplechar 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 **