C/C++ Programming Help Thread
This is a discussion on C/C++ Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Zitat von TMNT I'm having problems with file i/o. I haven't had this problem before, and this code always worked ...
-
12-29-2007, 08:06 PM #7291QJ Gamer Blue
- Registriert seit
- Sep 2005
- Ort
- Chigasaki, Japan
- Beiträge
- 226
- Points
- 4.980
- Level
- 45
- Downloads
- 0
- Uploads
- 0
Looks fine to me (you can omit the fseek() though). To be sure I ran this on a completely different system and it does what you intended to do (i.e. replace the first character in the file with the letter 's').
Zitat von TMNT
Are you sure the file isn't read-only? Also, is options non-null as result of fopen()?
-
12-29-2007, 08:29 PM #7292It's good to be free...

- Registriert seit
- Feb 2007
- Beiträge
- 2.440
- Points
- 10.420
- Level
- 67
- Downloads
- 0
- Uploads
- 0
It would probably crash if options was null.
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
-
12-29-2007, 09:21 PM #7293QJ Gamer Bronze

- Registriert seit
- Aug 2007
- Ort
- Australia
- Beiträge
- 659
- Points
- 8.045
- Level
- 60
- Downloads
- 0
- Uploads
- 0
Thanks, i was wondering how it worked
Zitat von Raphael
-
12-30-2007, 07:49 AM #7294
sceIoRead(file2, buffer+10, 10);
Zitat von Xsjado7
It work like this:
It writes to the initial pointer address of buffer + 10; pointer math.
-
12-30-2007, 09:44 AM #7295QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
No, it's obviously not a result of being null. However, i am opening and closing the file constantly in my program, but if i make a separate txt file for this code only (example - options2), then the code works flawlessly. So my guess is it's because i am opening and closing the file a lot? Can anyone explain this to me?
Zitat von kuroneko
Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 09:45 AM #7296It's good to be free...

- Registriert seit
- Feb 2007
- Beiträge
- 2.440
- Points
- 10.420
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Note: ((int*)0x80000000) + 8 == (int*)0x80000020. It does not equal (int*)0x80000008, as one might expect. It increments the pointer by the sizeof what it points to.
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
-
12-30-2007, 10:32 AM #7297QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
Could someone help me out with this txt problem? Here is my FULL game code. Also, try to tell me how i optimize and remove unnecessary code.
http://rafb.net/p/LjnFOH38.htmlCalypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 10:43 AM #7298QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
1. Indent your code properly so people can actually follow it: http://rafb.net/p/VCX2NU54.html
2. Just post the code with the problem, a ~1700 line single file isn't easy to follow.
3. Why not just open the file once, store the data and then refer to the data when you need it instead of opening the file again?
4. fprintf(options, "n"); actually puts 2 bytes into the file (strings are null terminated) not 1.[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 10:48 AM #7299QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
That's the way i always indent it (people have different styles). Lines 118-278, 1151-1156.
Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 10:51 AM #7300Banned for LIFE
- Registriert seit
- Oct 2006
- Ort
- East London, England
- Beiträge
- 2
- Points
- 18.744
- Level
- 86
- Downloads
- 0
- Uploads
- 0
Im really not understanding the sceIoLseek command. The header gives a very brief description but I dont get it.
-
12-30-2007, 10:53 AM #7301QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
I edited point 4 on my previous post:
4. fprintf(options, "n"); actually puts 2 bytes into the file (strings are null terminated) not 1.
People do have different styles but if you expect OTHER people to read it as well, you should use a STANDARD style such as ANSI or Kernighan & Ritchie or the very least, have a consistent style throughout the code submitted.[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 10:54 AM #7302It's good to be free...

- Registriert seit
- Feb 2007
- Beiträge
- 2.440
- Points
- 10.420
- Level
- 67
- Downloads
- 0
- Uploads
- 0
sceIoLseek is equivalent to fseek, except instead of returning success or failure, it returns the new position in the file. Use SEEK_CUR to seek from the current position, SEEK_SET to seek from the beginning of the file or SEEK_END to seek from the end of the file.
E] That PRNG (line 109) is broken.
rand_val = lo + rand_val % hi;
should be
rand_val = lo + (rand_val % (hi-lo));
E2]
Replace
fprintf(options, "1");
with
fputc('1',options);pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
-
12-30-2007, 10:58 AM #7303QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
Sorry about the indents. :o
Zitat von yaustar
Do you have a link to a guide where i can learn this file i/o? Read and Write? I always have trouble with this.Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:00 AM #7304It's good to be free...

- Registriert seit
- Feb 2007
- Beiträge
- 2.440
- Points
- 10.420
- Level
- 67
- Downloads
- 0
- Uploads
- 0
I've revised my previous post. This, in addition to the reply to the PM I sent you, should fix up the problems you alluded to (in addition to fixing your PRNG)
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
-
12-30-2007, 11:02 AM #7305QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
You should only seed the random number generator once, not everytime you want a random number.Code:static int GetRandomNum(int lo, int hi) { SceKernelUtilsMt19937Context ctx; sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME u32 rand_val = sceKernelUtilsMt19937UInt(&ctx); rand_val = lo + rand_val % hi; return (int)rand_val; }[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:05 AM #7306Banned for LIFE
- Registriert seit
- Oct 2006
- Ort
- East London, England
- Beiträge
- 2
- Points
- 18.744
- Level
- 86
- Downloads
- 0
- Uploads
- 0
So basically, the offset needed is just which number byte to start off from?
So if I want to start at the 800th byte I would just use the number 800?
-
12-30-2007, 11:05 AM #7307QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
http://www.cppreference.com/stdio/index.html in addition to the link IWN gave earlier to you.
Zitat von TMNT
Is what I normally use plus lots of experimentation and testing on the PC where I can debug more efficiently. Also, I personally prefer to use C++ file streams and ASCII text then C I/O and binary.[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:06 AM #7308QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
1) What is fputc? - nevermind, thanks for link yaustar
2) But i need the random number generator everytime i want a random number...Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:12 AM #7309QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
You perform this code once and store the context.Code:SceKernelUtilsMt19937Context ctx; sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:14 AM #7310QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
So what should i do to fix this problem?
Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:16 AM #7311QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Example:
Zitat von TMNT
Code:static int GetRandomNum( SceKernelUtilsMt19937Context * pCtx, int lo, int hi) { if( NULL == pCtx ) { return 0; } int rand_val = (int)sceKernelUtilsMt19937UInt(pCtx); rand_val = lo + ( rand_val % (hi-lo) ); return rand_val; } int main() { SceKernelUtilsMt19937Context ctx; sceKernelUtilsMt19937Init(&ctx, time(NULL)); // get random number GetRandomNum( &ctx, 0, 100 ) return 0; }[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:25 AM #7312QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
Yaustar, you smart, beastly, and ineffable genius......
I replaced fprintf with fputc, and i am getting a warning of being an incompatible pointer type.Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:35 AM #7313QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Can you post the full error and the block of code that produces it?
If you want to be even more clever with the GetRandomNum (and because it is in C), you can try this:
Please note, that if I was using C++, I wouldn't have written this code like this. I would use a class.Code:SceKernelUtilsMt19937Context InitialiseMtContext() { SceKernelUtilsMt19937Context ctx; sceKernelUtilsMt19937Init(&ctx, time(NULL)); return ctx; } int GetRandomNum(int lo, int hi) { static SceKernelUtilsMt19937Context ctx = InitialiseMtContext(); int rand_val = (int)sceKernelUtilsMt19937UInt(&ctx); rand_val = lo + ( rand_val % (hi-lo) ); return rand_val; } int main() { // get random number GetRandomNum( 0, 100 ); // ctx gets initialised here the first time the function is called GetRandomNum( 0, 100 ); // but not here GetRandomNum( 0, 100 ); // nor here return 0; }[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:40 AM #7314QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
Error:
Block of code:Code:main.c(1379) : warning: passing argument 1 of 'fputc' makes integer from pointer without a cast main.c(1379) : warning: passing argument 2 of 'fputc' from incompatible pointer type
Code:FILE* options; options = fopen("./files/options.txt","r+b"); fseek(options, 1, SEEK_SET); fputc(options, "1"); fclose(options);Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:43 AM #7315QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Should be:Code:fputc(options, "1");
You were trying to pass a string instead of a char.Code:fputc(options, '1');
Also note, you need to do the same for your CPU settings as well.[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:45 AM #7316QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
This makes so much more sense....Thanks....:)
Edit:
Same error!
Code:fputc(options, '1');
Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:46 AM #7317It's good to be free...

- Registriert seit
- Feb 2007
- Beiträge
- 2.440
- Points
- 10.420
- Level
- 67
- Downloads
- 0
- Uploads
- 0
If you're using SEEK_SET and you start counting from 0, yes.
Zitat von eldiablov
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
-
12-30-2007, 11:53 AM #7318QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
I'm still getting the same error Yaustar...
Calypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)
-
12-30-2007, 11:53 AM #7319QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
http://www.cppreference.com/stdio/fputc.html
That is why. Look at the parameter order.
Code:fputc('1', options);[Blog] [Portfolio]
[Homebrew Illuminati - Serious Homebrew Development Forums]
[I want to make Homebrew FAQ] [How I broke into the Games Industry]
[Programming Book List] [Programming Article List]
-
12-30-2007, 11:55 AM #7320QJ Gamer Silver

- Registriert seit
- May 2006
- Ort
- Behind you.
- Beiträge
- 1.814
- Points
- 10.921
- Level
- 69
- Downloads
- 0
- Uploads
- 0
I got it:
fputc('1', options);
Edit: Same time you posted i posted too (lol...):
http://www.mkssoftware.com/docs/man3/fputc.3.aspCalypso - Enjoy the excellent 2D space shooter:
http://dl.qj.net/Calypso-v1-PSP-Home...6542/catid/195
"Quoting yourself in your signature means you love to masterbate while looking at the mirror." -me (oh, wait...)


LinkBack URL
About LinkBacks
Mit Zitat antworten


Hello everyone I am new here and I am glad to be part of this amazing community and I think there...
New to forum