Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
March 13, 2010, 05:50:57 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Join our IRC channel: ##psp-programming on freenode
Home Help Search Shop Login Register
Digg This!
Pages: 1 [2] 3 4 ... 6
Print
Author Topic: [Tutorial] File I/O  (Read 36297 times)
harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #15 on: May 23, 2006, 10:23:28 PM »

ya, you have access to the database. pleaase edit my post soon :/
Logged


Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #16 on: May 24, 2006, 06:17:12 AM »

That was a pain in the ass  Confused
Logged

harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #17 on: May 24, 2006, 09:29:42 AM »

heh, yeh  Laughing

thanks though...
Logged
harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #18 on: May 30, 2006, 01:05:28 PM »

3rd and final tutorial in this series is being writen now.
expect it up by tonight (tomorow morning Wink)
Logged
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #19 on: May 30, 2006, 02:07:00 PM »

Ok, I'm working on getting yours, vaza's, and a few other misc tutorials into the tutorial section right now.
Logged

harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #20 on: May 30, 2006, 02:20:11 PM »

thanks, ive also got a nice little present for psp-programming Wink
Logged
harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #21 on: May 30, 2006, 11:56:20 PM »

File Input (Reading) - 3. Searching to a certain point.

Ok, so the basic idea for this tutorial is searching to a certain point inside a string, or file.
For instance, your file contains "bla;blah", you can search to the ";" and print everything after it, which would be "blah".
This can be useful when reading a file into an array etc...

So first we are going to open up testfile.txt in our favourity text edit which by now should be vim Smile
Inside this file we are going to have the example i just gave:
Code:
this is not usefull;super useful!



Now we are going to open up main.c, so go ahead and create/open up main.c.


Lets include some files:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

I shant go on about the includes anymore, you should know them by now.


start the main function:
Code:
int main() {

You should by now, if you have been following these tutorials what this means.


We are now going to declare some variables:
Code:
char c, *buffer;
int n = 0, x, size;
FILE *pFile;

,: We can use a simple comma to declare multiple things on one line, eg. "int foo, bar".
[200]: This shows that the variable, in this case being "buffer" can store up to 200 bytes of data.
The rest should be easy for you to understand.


We are now going to give the variable "pFile" a function.
Code:
pFile = fopen("test.txt", "rb");

So now, we have opened our file.


Now we check the file, too see if it opened correctly.
Code:
if(file == NULL) perror("Error opening file!");

perror, " Print error message". In other words, if the file is empty, print an error.
NULL, this is another way of saying empty, or 0... its "null".


We next use "else".
Code:
else {

In simple terms, this if and else at the moment is saying "if file is empty, print error, else do....", if its not, do foo.


Ok, now what? We know are going to check we are not at the end of the file in a loop, as we will be reading every char in the file searching for ";"..
Code:
while(c != EOF) {

!=, aka, is not... "while c is not EOF do foo..."
EOF, "End of File".
So this is doing "while c is not the end of the file do foo".


Next, we declare c.
Code:
c = fgetc(pFile);

fgetc, Get the next character from a stream". This will be explained later.


Now we are going to check if the character is ";".
Code:
if(c == ';') {
x = ftell(pFile);
}

This is saying, if c, as declared above to be the next char is the same as ";", x = ftell(pFile). ftell(pFile) will be the length of bytes from where you are to the start of the file. This means, x now has the byte where the ";" is located.


Close the while loop:
Code:
}


So, what have we done so far?
In english:
Quote
"Opened a file, and searched through it byte by byte to file the character ";", if we have found it we set a variable with its location on it (bytes in)".



We are now going to find out the file size.
Code:
fseek(pFile, 0, SEEK_END);
size = ftell(pFile);
rewind(pFile);

I have already explained this code in my last tutorial(s).


We also need to free the memory for the file to go into.
Code:
buffer = (char*) malloc(size-x);
if(buffer == NULL) perror("Error allocating memory!");

size-x, the original size minus the location of the char ";" is how much data is left in the file.
I have also explained the rest in my last tutorial(s).


Next we are going to seek the "x" bytes. We have already declared the "x" variable as the location of the char ";".
Code:
fseek(pFile, x, SEEK_SET);

SEEK_SET, the start.


We are now going to load the rest of the file into "buffer".
Code:
fread(buffer, 1, size-x, pFile);

buffer, destination.
size-x, the original size minus the location of the char ";" is how much data is left in the file.
pFile, location.


We can know close the file:
Code:
fclose(pFile);



So, what have we done so far?
Quote
We have loaded a file, searched through it in a "while" loop to find the ";" char. If found, the variable "x" is set to its location (byte). We have then checked the file size (minus "x") and freed that much in the memory. Then we used fseek to seek to "x"'s position in the file (after ";") and then loaded the rest into the buffer.

So, what next you ask?


We are now going to give as much information as possible, so you can see how to use it for later use.
Code:
printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer);

This is going to print the file size in bytes, the location/byte of the char ";", bytes left after ";" and the text after ";".


Free the buffer from the memory:
Code:
free(buffer);



End the if / else:
Code:
}


Return 0...
Code:
return 0


Finally, end the main function:
Code:
}



This is what your main.c file should look like:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
        char c, *buffer;
        int n = 0, x, size;
        FILE *file;
        file = fopen("test.txt", "rb");
        if(file == NULL) perror("Error opening file!");
        else {
                while(c != EOF) {
                        c = fgetc(file);
                        if(c == ';') {
                                x = ftell(file);
                        }
                }
                fseek(file, 0, SEEK_END);
                size = ftell(file);
                rewind(file);

                buffer = (char*) malloc(size-x);
                if(buffer == NULL) perror("Error allocating memory!");

                fseek(file, x, SEEK_SET);
                fread(buffer, 1, size-x, file);
                fclose(file);
                printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer);
                free(buffer);
        }
        return 0;
}



And your test.txt:
Code:
this is not usefull;super useful!



Ok, so now compile your program (gcc!)... and when it has finished, run the binary it makes, you should get the following output:
Code:
File Size: 34
Byte: 20
Bytes Left: 14
Text After: super useful!



Awesom, eh?


I hope you enjoyed reading my "File Reading" tutorials, and that you found them helpfull.

--harleyg
Logged
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #22 on: May 31, 2006, 08:59:10 AM »

Fixed the problem =)

Stupid apache mods...
Logged

harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #23 on: May 31, 2006, 09:00:24 AM »

oh sexay !

yay, finally, it is completed!

so i can post alot now and not get an error?

edit: just checked, **** yeh!
Logged
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #24 on: May 31, 2006, 02:18:10 PM »

Are there any extra LIBS that need to be linked in the Makefile?  (I'm adapting it to be PSP C code)

Edit:
Here's what I've got: http://www.psp-programming.com/tutorials/c/file_input_1.htm
Logged

Ryalla
Miinaturvat Rules
Global Moderator
Hero Member
*

Karma: +50/-12
Offline Offline

Posts: 813
1707.65 points

View Inventory
Send Money to Ryalla


View Profile
« Reply #25 on: May 31, 2006, 02:21:22 PM »

Quote from: "Yeldarb"
Are there any extra LIBS that need to be linked in the Makefile?  (I'm adapting it to be PSP C code)

Edit:
Here's what I've got: http://www.psp-programming.com/tutorials/c/file_input_1.htm
Nope. I just tested them out. All work 100% right off the bat. There's no makefile needed if you just gcc it. ;D
Logged

"A year spent in artificial intelligence is enough to make one believe in God."
harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #26 on: May 31, 2006, 02:24:31 PM »

yes, and it wasnt aimed at the psp either, so can you remove the makefile and like.. leave it how it is, please...
Logged
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #27 on: May 31, 2006, 02:30:42 PM »

It's gotta be aimed at the PSP, this is PSP-programming.com after all Wink

If it's a general C tutorial and I put it up all that's going to happen is the forums will be flooded with people saying "I can't get this to work on my PSP"
Logged

harleyg
Give miinaturvat Points!
Hero Member
*****

Karma: +11/-14
Offline Offline

Posts: 715
462.95 points

View Inventory
Send Money to harleyg


View Profile
« Reply #28 on: May 31, 2006, 02:34:39 PM »

just say its general C code, which can be used on the psp but that it is generally aimed at developing the C skills, not the C-PSP skills.
Logged
Yeldarb
Miinaturvat Rules!
Administrator
Hero Member
*

Karma: +16/-3
Offline Offline

Posts: 602
4160.65 points

View Inventory
Send Money to Yeldarb


View Profile WWW
« Reply #29 on: May 31, 2006, 02:36:16 PM »

I just spent an hour clarifying, modifying, converting and adding to it; it can do both at the same time.  It's set to be a PSP-C tutorial as well as a general C tutorial.

There are plenty of sites for general C tutorials, this site is about the PSP.
Logged

Pages: 1 [2] 3 4 ... 6
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.209 seconds with 39 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com