File Input 1
Reading an Entire File
Read an entire file into memory, print the contents, and terminate the file.
This is the second part of the File Input Tutorial contributed by harleyg of PSP-Programming.com's forums (originally posted as generic C code here).
Now, start your main function:
pspDebugScreenInit();
SetupCallbacks();
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".
{ is used to show that the function has started!
For more info about the other two lines, you'll need to read the other PSP C tutorials.
Next we will declare some variables:
long lSize;
char * buffer;
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).
char*, we use this to declare the variable "buffer." It can contain characters (a-z, A-Z, symbols, numbers, etc).
Then we give the pFile variable some information:
pFile, this is the variable we already made into a "file pointer" above.
=, the assignment operator sets "foo" equal to "bar" (foo = bar).
fopen, this is a function in C that is used to open a file (file open). In requires 2 parameters, the "filename" and "mode".
"myfile.txt", this is the first parameter, the "filename."
rb, this contains the two modes we are using:
- r - Open a file for reading. The file must exist.
- b - Binary mode. End of file is reached at the last byte of the file. No conversions.
The fopen command gets everything set up for reading. It then returns an identifier pointing to the file (this is what pFile now stores). We can now access the file we just opened by addressing our variable "pFile."
Now we need to check if the file has any data inside. We do this like:
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.
==, is a comparisson operator. It evaluates to "true" if the two arguments on either side of it are equal to each other.
Note the difference between the assignment operator (=) and equality operator (==).
NULL, this means "empty," or "nothing," or "not set."
sceKernelExitGame(), this terminates the process (or exits the program).
So, this entire if statement means in english: "if pFile is empty, exit the program."
That concludes part two, continue with part three.
