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 21, 2010, 11:28:25 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 5 6
Print
Author Topic: [Tutorial] File I/O  (Read 36429 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 #30 on: May 31, 2006, 02:37:21 PM »

but that is not what i wrote...
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 #31 on: May 31, 2006, 02:38:30 PM »

I understand that...  it's just that I have to make this site have some sort of continuity.
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 #32 on: May 31, 2006, 02:45:13 PM »

but i dont even see how it can be used as a fully working binary let alone a PSP binary, its a few lines of example code to teach people how to use the function, on the computer or PSP...

i can see your point about psp-programming.com, so ok sure, you can change them but can you link to the post of whatever tutorial it is in this thread (links in 1st post) on the tutorials, so these ones are kept intact?
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 #33 on: May 31, 2006, 02:47:12 PM »

Yeah, I can do that.

They don't need to be fully functional full-scale applications (look at lessons 1 through 6, none of them give you something "useful"), but when people do the tutorials they want to end up with an end product; since this site is all about programming for the PSP, it's fitting that they end up with a PSP binary.
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 #34 on: May 31, 2006, 02:52:24 PM »

gotcha Smile
Logged
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #35 on: May 31, 2006, 03:21:33 PM »

I am with yeldarb on this one, i want to come here looking for a simple tutorial without having to go through and convert basic C code to PSP specific code... (exit(1) = sceKernelExitGame(), i think you forgot to switch these yeldarb?)
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 #36 on: May 31, 2006, 03:22:20 PM »

im not arguing anymore sg57.
Logged
SG57
Sr. Member
****

Karma: +7/-37
Offline Offline

Posts: 474
1140.80 points

View Inventory
Send Money to SG57


View Profile
« Reply #37 on: May 31, 2006, 03:25:25 PM »

i know, just putting my thoughts out there  Embarassed  is that wrong? Crying or Very sad
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 #38 on: May 31, 2006, 03:27:10 PM »

extreamly, bad boy dirty boy in your bed! (harry hill)

anywho, sleep.
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 #39 on: June 01, 2006, 01:11:35 AM »

File Output (Writing) - 1. Writing a block of data to a file.
What we will learn: How to create a file, and print something simple into it using fwrite.


Ok, so once again we open up our favourite text editor and create our main.c file. Once you have done that, we need some includes!
Code:
#include <stdio.h>
#include <stdlib.h>

I have already explained these includes! We may not need stdlib, but meh Smile


So know we start our main function:
Code:
int main(void) {


Next we declare some variables.
Code:
FILE * pFile;
char buffer[] = "Humpty Dumpty sat on the wall";

Here we are saying pFile is a file pointer and buffer contains chars, we can also say what chars it contains, eg. "char foo[] = "blah".


Next we need to open our file...
Code:
pFile = fopen ("myfile.txt" , "w");

As you can see, we are opening the file for writing insted of for reading!
w, "Create an empty file for writing. If a file with the same name already exists its content is erased".


Next we use a shiny new function! ooOOoo... Smile
Code:
fwrite (buffer , 1 , 29 , pFile);

fwrite, "Write a block of data to a stream". This function requires 4 parameters: buffer, size, count and stream
buffer, this coincidently is the first parameter, the "buffer". This is the pointer to the data to be written. So whatever is in "buffer" is going to be writen to the file.
1, This is th second parameter, the "size". "Size in bytes of each item that has to be written".
29, This is the penultimate parameter. the "count". "Number of items, each one with a size of size bytes". This is how many bytes from the buffer do you want to write to the file. In this case, we want all of it and the buffer is 29 bytes.
pFile, This is the final parameter, the "stream". "pointer to an open file with writing access". As you can see i made "with writing access" bold, this is to show that the file must of been opened with writing access.


So, now we have had a new function, what now?!

The same as normal, we just close the file and return 0 to show that all went well.
Code:
fclose (pFile);
return 0;



Close the main function:
Code:
}



Here is what your code should look like:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE * pFile;
char buffer[] = "Humpty Dumpty sat on the wall";
pFile = fopen ("myfile.txt" , "w");
fwrite (buffer , 1 , 29 , pFile);
fclose (pFile);
return 0;
}



Now, compile this with your compiler (gcc!) and execute the output binary, you should be left with a file called "myfile.txt" which inside says: "Humpty Dumpty sat on the wall".

Yes, its as simple as that, simpler than reading a file actually!

Congratulations! You just wrote to a file in C!

--harleyg
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 #40 on: June 01, 2006, 02:09:54 AM »

File Output (Writing) - 2. Writing formatted data to a file.
What you will learn: How to print formatted text to a file with fprintf.


Ok, so first we need to do some includes.
Code:
#include <stdio.h>
#include <stdlib.h>


Yes yes, and now start the main function:
Code:
int main(void) {


We know declare only one variable! yay!
Code:
FILE * pFile;

We set pFile as a file pointer, lovely.

We know open the file we want as normal, and yet again we use "w" so it is writable.
Code:
pFile = fopen ("myfile.txt","w");



Once again, a sexy new function, YAY!!!
Code:
fprintf (pFile, "I printed this using fprintf!\n");

fprintf, this can print formatted text yet is is SO much easyer to use. It only requires 2 parameters: "stream" and "format".
pFile, the stream parameter, a pointer to an open file.
"*", the rest is exactly the same as printf, the use of "%s / %i", etc! Refer to google for help with printf.

So yet again being normal, we close the file and return 0:
Code:
fclose(pFile);
return 0;


End the main function:
Code:
}


Here is what your code should look like:
Code:
#include <stdio.h>
#include <stdlib.h>

int main () {
FILE * pFile;
pFile = fopen ("myfile.txt","w");
fprintf (pFile, "I printed this using fprintf!\n");
fclose (pFile);
return 0;
}


Once again we compile it and execute the binary. If all goes well, you should be left with a file called "myfile.txt" and it contains: "I printed this using fprintf!".

As you can see there is a major difference between fprintf and fwrite, where fwrite has to know how many bytes to print and a pointer to one variable, fprintf can print multiple variables, and plain text into a file with no fuss. These tutorials are getting easyer and easyer eh?


Congratulations!!! You just wrote to a file using fprintf!


--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 #41 on: June 01, 2006, 08:32:27 AM »

Nice additions =)

(Did you see the there's a link to this thread on the PSPU homepage?)
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 #42 on: June 01, 2006, 08:59:51 AM »

Quote from: "Yeldarb"
Nice additions =)

(Did you see the there's a link to this thread on the PSPU homepage?)
I say you should get it removed. </3 PSPUpdates is full of a bunch of n00bs.
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 #43 on: June 01, 2006, 09:01:12 AM »

nahh the more traffic the better, also im so happy now because they banned me for a few days *again* and now they have to post something i done on the front page! muhahaha
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 #44 on: June 01, 2006, 09:07:34 AM »

Quote from: "harleyg"
nahh the more traffic the better, also im so happy now because they banned me for a few days *again* and now they have to post something i done on the front page! muhahaha
Revenge'd. Even though it's revenge, PSPU still sucks..

OT: Nathan's avatar scares me. T_T
Logged

"A year spent in artificial intelligence is enough to make one believe in God."
Pages: 1 2 [3] 4 5 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.207 seconds with 38 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com