File Input 1
Reading an Entire File
Read an entire file into memory, print the contents, and terminate the file.
This is the third part of the File Input Tutorial contributed by harleyg of PSP-Programming.com's forums (originally posted as generic C code here). Parts one and two need to have been completed before proceeding.
Now that we've got that covered, we are going to check the file size.
lSize = ftell (pFile);
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).
( ... ), again, the parenthesis contain the parameters.
0, this is the offset, zero means we want to start at the beginning.
SEEK_END, we are saying here that we want to read to the end of the file.
lSize = ftell (pFile);, we are going to store the return value of ftell() to lSize.
ftell(pFile);, "Return the current position in a stream". ftell only requires one parameter, a pointer to an open file, in this case it is pFile. ftell's main function is to tell you how many bytes until the begining from the position you are at. Since we're at the end (we read to the end with fseek) it tells us the file's size.
rewind (pFile);, rewind's function is to reopen the file back to the beginning. It also only requires one parameter, which is a pointer to an open file. So now the file is primed for use again.
So, here's a recap of what we just did to find the length of the file: Open the file, seek the end, then find how many bytes away from the beginning we are. So if the file is 10 bytes, we'd go to the end and count back to the begining, finding that it is 10 bytes away. We stored this value into the variable lSize.
Next we need to allocate enough memory to fit the file into.
if (buffer == NULL) sceKernelExitGame();
char *, this is typesetting malloc to tell it that it contains characters.
malloc, this is a function to allocate memory (eg, malloc(56); will give you 56 bytes of memory for you to store information in). So malloc (lSize) is allocating space for the entire file size.
sceKernelExitGame(), (I've already explained this, refer to where we checked to make sure the file wasn't empty for information).
Ok, so now we have successfully opened the file, checked its size and allocated space for it in the memory.
Next, we copy the file contents into the buffer (memory).
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.
1, this is the second parameter, the "size". This is the size in bytes of each item to be read. Since characters are one byte long, we use a "1" here.
lSize, this is the third parameter, "count". This is the amount of characters to read into the buffer, in this case its lSize, the file size.
pFile, the fourth and final parameter "stream". This is the pointer to an open file, ours is pFile.
Ok, so now we have just read the file into the memory. We have a string of the file in our "buffer" variable.
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.
fclose (pFile);, "flushes any buffers maintained for the given file and closes the file." Pretty self explanatory, eh?
Good, you've made it through part three! Now on to part four.