View Single Post
 
Old 07-10-2007, 07:58 PM
pirata nervo pirata nervo is offline
Moderator
PSP Titan
 
Join Date: Mar 2007
Location: www.consoleworld.net
Posts: 5,008
pirata nervo is on a distinguished road
Default

Sorry but i forgot yo copy this fro mmy forum.

Code:
// libraries that we will use
#include <pspkernel.h>
#include <pspdebug.h>
#include <stdio.h>
#include <stdlib.h>
#include <pspctrl.h>
 
//sceModuleInfo
PSP_MODULE_INFO("io", 0, 1, 1);

//Callbacks

#define printf pspDebugScreenPrintf /* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
          sceKernelExitGame();
          return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}


int main(void)
{
    pspDebugScreenInit();
    SetupCallbacks();
    
    SceCtrlData pad;
    

    FILE * myFile;
    char write[] = "wow i used the input/output commands!";
    myFile = fopen ("file.txt" , "r+");
    
    
    
     
    while(1)
    {
            
              
            sceCtrlReadBufferPositive(&pad, 1);
     
            if(pad.Buttons & PSP_CTRL_CROSS)
            {
                           fwrite (write , 1 , 37 , myFile);
                           fclose (myFile);
                           printf("Press O to read what you wrote.");
            }
            
            if(pad.Buttons & PSP_CTRL_CIRCLE && myFile != NULL)
            {
                           FILE * pFile;
                           long lSize;
                           char * read;
                           pFile = fopen ("file.txt" , "r");
                           fseek (pFile , 0 , SEEK_END);
                           lSize = ftell (pFile);
                           rewind (pFile);
                           read = (char*) malloc (lSize);
                           fread(read, 1, lSize, pFile);
                           fclose (pFile);
                           printf("%s\n", read);
                           
            }
            
            
            if(pad.Buttons & PSP_CTRL_TRIANGLE)
            {
                           sceKernelExitGame();
            }  
     }
     
     sceKernelSleepThread();
     return 0;
}
Explanation(only for you)

int main(void) main function
{
pspDebugScreenInit();
SetupCallbacks();

SceCtrlData pad;


FILE * myFile; declaration of the pointer myFiler

char write[] = "wow i used the input/output commands!"; declaration of the variable write

myFile = fopen ("file.txt" , "r+"); here we set the adress of the pointer myFile and the mode - read and write mode - "r+"

while(1) main loop
{

sceCtrlReadBufferPositive(&pad, 1); read the buffer of the psp buttons

if(pad.Buttons & PSP_CTRL_CROSS) if pressed/held the X button
{
fwrite (write , 1 , 37 , myFile); it will write in the file the string write
fwrite is the function to write files
write is the variable that will be written in the files
1 is the size in bytes of each item to be written. Since characters are one byte long, we use a "1" here.
fclose (myFile); close the file like using notepad
you need to close the file cuz we will need it to open it again but in read mode
printf("Press O to read what you wrote."); print on the screen "Press O to read what you wrote."
}

if(pad.Buttons & PSP_CTRL_CIRCLE && myFile != NULL) if the O button is pressed/held
{
FILE * pFile; delcaration of the variable pFile as pointer
long lSize; declaration of the variable long int lSize
char * read; declaration of the variable read
we use the * to declare the variable "read", now, can contain characters (a-z, A-Z, symbols, numbers, etc).

pFile = fopen ("file.txt" , "r"); here we set the adress of the pointer pFile and the mode - read mode - "r"

fseek (pFile , 0 , SEEK_END); this function requires 3 parameters, the pointer, where starts the file(begining -> 0) and the seek_end -> end of the file
lSize = ftell (pFile); this, store the return value of ftell() to lSize.
rewind (pFile); rewind's function is to reopen the file back to the beginning
read = (char*) malloc (lSize); read =, we are setting the value of the variable "read."
char *, this is typesetting malloc to tell it that it contains characters.
malloc, this is a function to allocate memory.


fread(read, 1, lSize, pFile); it will read the file and copy the contents to the long int lSize
fread is the function to read files
read is the variable that will receive the contents of the file
1 is the size in bytes of each item to be read. Since characters are one byte long, we use a "1" here.

fclose (pFile); close the file like using notepad
printf("%s\n", read);

}


if(pad.Buttons & PSP_CTRL_TRIANGLE)
{
sceKernelExitGame(); exit to XMB
}
}

sceKernelSleepThread();
return 0;
}

thanks, and please comment,


Download from here the source and the txt file:
http://rapidshare.com/files/42206729/io.rar
__________________

Upgrade your PSP Slim or FAT now!
NervOS Official Forum
Reply With Quote