File Input 1
Reading an Entire File
Read an entire file into memory, print the contents, and terminate the file.
Tutorial contributed by harleyg of PSP-Programming.com's forums (originally posted as generic C code here).
In this tutorial series, we will be covering the basics of file reading in C.
We will be using the following functions: fopen, fread, fseek fclose, fgetc, malloc. And we will also use some simple math and variables.
This first tutorial covers printing an entire file to the screen. We will learn how to read an entire file into memory, how to print the contents to the screen, and how to terminate the file. This will give you the basic foundation for File I/O (Input / Output) which we will build upon in later parts of this tutorial series.
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:
#include <pspdebug.h>
#include <stdio.h>
#include <stdlib.h>
These includes do the following:
- pspkernel.h and pspdebug.h: Check out the general PSP C tutorials for information.
- stdio.h: "standard input-output header," is the library in C programming language which contains functions for manipulating standard input and output.
- stdlib.h: "general purpose standard", is the library in C programming language which includes functions involving memory allocation, process control, conversions and others general purpose functions.
Ok, enough about includes already. I see you rolling your eyes at me... not cool.
Here we insert our standard callbacks and setup. Check out the general PSP C tutorials for information.
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;
}
