Editing File Input Tutorial 1 (Complete Files)

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 9: Line 9:
First, we will need to make a few include statements, so open up your favourite text editor (I'm partial to vim) and create a new file called main.c. Now, inside the text editor, type:
First, we will need to make a few include statements, so open up your favourite text editor (I'm partial to vim) and create a new file called main.c. Now, inside the text editor, type:


<pre>#include <pspkernel.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdebug.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h></pre>
#include <stdlib.h>
These includes do the following:
These includes do the following:


Line 22: Line 22:
Here we insert our standard callbacks and setup. Check out the general PSP C tutorials for information.
Here we insert our standard callbacks and setup. Check out the general PSP C tutorials for information.


<pre>#define printf pspDebugScreenPrintf /* Exit callback */
#define printf pspDebugScreenPrintf /* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
int exit_callback(int arg1, int arg2, void *common) {
           sceKernelExitGame();
           sceKernelExitGame();
Line 50: Line 50:


           return thid;
           return thid;
}</pre>
}


Now, start your main function:
Now, start your main function:


<pre>int main(void) {
int main(void) {
     pspDebugScreenInit();
     pspDebugScreenInit();
     SetupCallbacks();</pre>
     SetupCallbacks();
int meaning interger is used to refer to any data type which can represent some subset of the mathematical integers.
int meaning interger is used to refer to any data type which can represent some subset of the mathematical integers.
void is used because in earlier versions of C, functions with no specific result defaulted to a return type of "int" and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to "char".
void is used because in earlier versions of C, functions with no specific result defaulted to a return type of "int" and functions with no arguments simply had empty argument lists. Pointers to untyped data were declared as integers or pointers to "char".
Line 64: Line 64:
Next we will declare some variables:
Next we will declare some variables:


<pre>FILE * pFile;
FILE * pFile;
long lSize;
long lSize;
char * buffer;</pre>
char * buffer;
FILE, we use this to make the pFile variable a "file pointer".
FILE, we use this to make the pFile variable a "file pointer".
long, now this is tricky as int and long int both can be from -2,147,483,648 to +2,147,483,647. I would normally use int, but for this tutorial, I thought it would be good to introduce this datatype. int is an explained above. Basically, long can store bigger numbers than int, but it also takes up more space in memory (it's a tradeoff).
long, now this is tricky as int and long int both can be from -2,147,483,648 to +2,147,483,647. I would normally use int, but for this tutorial, I thought it would be good to introduce this datatype. int is an explained above. Basically, long can store bigger numbers than int, but it also takes up more space in memory (it's a tradeoff).
Line 73: Line 73:
Then we give the pFile variable some information:
Then we give the pFile variable some information:


<pre>pFile = fopen ("myfile.txt" , "rb");</pre>
pFile = fopen ("myfile.txt" , "rb");
pFile, this is the variable we already made into a "file pointer" above.
pFile, this is the variable we already made into a "file pointer" above.
=, the assignment operator sets "foo" equal to "bar" (foo = bar).
=, the assignment operator sets "foo" equal to "bar" (foo = bar).
Line 86: Line 86:
Now we need to check if the file has any data inside. We do this like:
Now we need to check if the file has any data inside. We do this like:


<pre>if (pFile==NULL) sceKernelExitGame();</pre>
if (pFile==NULL) sceKernelExitGame();
if, an "if statement" can be used to compare things, (eg. if "foo" is equal to "bar").
if, an "if statement" can be used to compare things, (eg. if "foo" is equal to "bar").
( ... ), the comparisson goes between the parenthesis. If it evaluates to "true," then the code following is executed.
( ... ), the comparisson goes between the parenthesis. If it evaluates to "true," then the code following is executed.
Line 97: Line 97:
Now that we've got that covered, we are going to check the file size.
Now that we've got that covered, we are going to check the file size.


<pre>fseek (pFile , 0 , SEEK_END);
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
lSize = ftell (pFile);
rewind (pFile);</pre>
rewind (pFile);
fseek, "Reposition stream's position indicator." As you probably guess, this function "seeks" trhough the file. fseek requires 3 parameters, he pointer to an open file, the offset (how many bytes from the beginning of the file to start) and the origin (or the end).
fseek, "Reposition stream's position indicator." As you probably guess, this function "seeks" trhough the file. fseek requires 3 parameters, he pointer to an open file, the offset (how many bytes from the beginning of the file to start) and the origin (or the end).
( ... ), again, the parenthesis contain the parameters.
( ... ), again, the parenthesis contain the parameters.
Line 112: Line 112:
Next we need to allocate enough memory to fit the file into.
Next we need to allocate enough memory to fit the file into.


<pre>buffer = (char*) malloc (lSize);
buffer = (char*) malloc (lSize);
if (buffer == NULL) sceKernelExitGame();</pre>
if (buffer == NULL) sceKernelExitGame();
buffer =, we are setting the value of the variable "buffer."
buffer =, we are setting the value of the variable "buffer."
char *, this is typesetting malloc to tell it that it contains characters.
char *, this is typesetting malloc to tell it that it contains characters.
Line 122: Line 122:
Next, we copy the file contents into the buffer (memory).
Next, we copy the file contents into the buffer (memory).


<pre>fread(buffer, 1, lSize, pFile);</pre>
fread(buffer, 1, lSize, pFile);
fread, "Read block of data from a stream into memory". This function requires 4 parameters.
fread, "Read block of data from a stream into memory". This function requires 4 parameters.
buffer, the first parameter, the "buffer", which coincidently is named "buffer." Basically, this is where we're storing the information in.
buffer, the first parameter, the "buffer", which coincidently is named "buffer." Basically, this is where we're storing the information in.
Line 133: Line 133:
Now, we need to close the file since it is not needed anymore. You always, always want to close your file. If you don't, bad things can happen.
Now, we need to close the file since it is not needed anymore. You always, always want to close your file. If you don't, bad things can happen.


<pre>fclose(pFile);</pre>
fclose(pFile);
fclose (pFile);, "flushes any buffers maintained for the given file and closes the file." Pretty self explanatory, eh?
fclose (pFile);, "flushes any buffers maintained for the given file and closes the file." Pretty self explanatory, eh?


ext we are going to print the buffer/file contents to the terminal.
ext we are going to print the buffer/file contents to the terminal.


<pre>printf("%s\n", buffer);</pre>
printf("%s\n", buffer);
This will print the buffer to the screen. If you don't understand how, check out the other C tutorials.
This will print the buffer to the screen. If you don't understand how, check out the other C tutorials.


Next, we free the memory we allocated.
Next, we free the memory we allocated.


<pre>free(buffer);</pre>
free(buffer);
free, is a function used to un-allocate memory you allocated. It requires one parameter, the variable you want to release.
free, is a function used to un-allocate memory you allocated. It requires one parameter, the variable you want to release.


We now end the thread return 0. This shows that everything ran fine if it got to this point. And signals the end of our main function.
We now end the thread return 0. This shows that everything ran fine if it got to this point. And signals the end of our main function.


     <pre>sceKernelSleepThread();
     sceKernelSleepThread();
     return 0;
     return 0;
}</pre>
}
And finally we closed the main function with an end bracket.
And finally we closed the main function with an end bracket.


Your final code should look like this:
Your final code should look like this:


<pre>#include <pspkernel.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdebug.h>
#include <stdio.h>
#include <stdio.h>
Line 194: Line 194:
     sceKernelSleepThread();
     sceKernelSleepThread();
     return 0;
     return 0;
}</pre>
}


You'll need your standard Makefile (place it in a new textfile called "Makefile" with no extention):
You'll need your standard Makefile (place it in a new textfile called "Makefile" with no extention):


<pre>TARGET = file1
TARGET = file1
OBJS = main.o
OBJS = main.o


Line 209: Line 209:


PSPSDK=$(shell psp-config --pspsdk-path)
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak</pre>
include $(PSPSDK)/lib/build.mak
Then just type "make" in CYGWIN and you're set.
Then just type "make" in CYGWIN and you're set.


Please note that all contributions to PSP Developer wiki are considered to be released under the GNU Free Documentation License 1.2 (see PSP Developer wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)