Seite 244 von 340 ErsteErste ... 144 194 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 294 ... LetzteLetzte
Zeige Ergebnis 7.291 bis 7.320 von 10174

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 ...

  
  1. #7291
    QJ Gamer Blue
    Points: 4.980, Level: 45
    Level completed: 15%, Points required for next Level: 170
    Overall activity: 0%

    Registriert seit
    Sep 2005
    Ort
    Chigasaki, Japan
    Beiträge
    226
    Points
    4.980
    Level
    45
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von TMNT
    I'm having problems with file i/o. I haven't had this problem before, and this code always worked fine until now:

    Code:
          FILE* options;
          options = fopen("./files/options.txt","r+b");
          fseek(options, 0, SEEK_SET);
          fprintf(options, "s");
          fclose(options);
    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').

    Are you sure the file isn't read-only? Also, is options non-null as result of fopen()?



  2. #7292
    It's good to be free...
    Points: 10.420, Level: 67
    Level completed: 93%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    2.440
    Points
    10.420
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    It would probably crash if options was null.
    pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ

  3. #7293
    QJ Gamer Bronze
    Points: 8.045, Level: 60
    Level completed: 48%, Points required for next Level: 105
    Overall activity: 0%

    Registriert seit
    Aug 2007
    Ort
    Australia
    Beiträge
    659
    Points
    8.045
    Level
    60
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von Raphael
    The first answer is correct. If you want to store the second file's ten bytes after the first, you'd have to do:
    Code:
    sceIoRead(file2, buffer+10, 10);
    Thanks, i was wondering how it worked

  4. #7294
    QJ Gamer Gold
    Points: 14.678, Level: 78
    Level completed: 57%, Points required for next Level: 172
    Overall activity: 0%

    Registriert seit
    Nov 2006
    Beiträge
    1.523
    Points
    14.678
    Level
    78
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von Xsjado7
    Thanks, i was wondering how it worked
    sceIoRead(file2, buffer+10, 10);
    It work like this:

    It writes to the initial pointer address of buffer + 10; pointer math.

  5. #7295
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von kuroneko
    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').

    Are you sure the file isn't read-only? Also, is options non-null as result of fopen()?
    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?
    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...)

  6. #7296
    It's good to be free...
    Points: 10.420, Level: 67
    Level completed: 93%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    2.440
    Points
    10.420
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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 əʞɒʇ

  7. #7297
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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.html
    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...)

  8. #7298
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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.

  9. #7299
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  10. #7300
    Banned for LIFE
    Points: 18.744, Level: 86
    Level completed: 79%, Points required for next Level: 106
    Overall activity: 0%

    Registriert seit
    Oct 2006
    Ort
    East London, England
    Beiträge
    2
    Points
    18.744
    Level
    86
    Downloads
    0
    Uploads
    0

    Standard

    Im really not understanding the sceIoLseek command. The header gives a very brief description but I dont get it.

  11. #7301
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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.

  12. #7302
    It's good to be free...
    Points: 10.420, Level: 67
    Level completed: 93%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    2.440
    Points
    10.420
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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 əʞɒʇ

  13. #7303
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von yaustar
    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.
    Sorry about the indents. :o
    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...)

  14. #7304
    It's good to be free...
    Points: 10.420, Level: 67
    Level completed: 93%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    2.440
    Points
    10.420
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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 əʞɒʇ

  15. #7305
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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;
    }
    You should only seed the random number generator once, not everytime you want a random number.

  16. #7306
    Banned for LIFE
    Points: 18.744, Level: 86
    Level completed: 79%, Points required for next Level: 106
    Overall activity: 0%

    Registriert seit
    Oct 2006
    Ort
    East London, England
    Beiträge
    2
    Points
    18.744
    Level
    86
    Downloads
    0
    Uploads
    0

    Standard

    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?

  17. #7307
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von TMNT
    Sorry about the indents. :o
    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.
    http://www.cppreference.com/stdio/index.html in addition to the link IWN gave earlier to you.

    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.

  18. #7308
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  19. #7309
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Code:
    SceKernelUtilsMt19937Context ctx;
        sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
    You perform this code once and store the context.

  20. #7310
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  21. #7311
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von TMNT
    So what should i do to fix this problem?
    Example:
    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;
    }

  22. #7312
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  23. #7313
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    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:
    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;
    }
    Please note, that if I was using C++, I wouldn't have written this code like this. I would use a class.

  24. #7314
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    Error:
    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
    Block of code:
    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...)

  25. #7315
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Code:
    fputc(options, "1");
    Should be:
    Code:
    fputc(options, '1');
    You were trying to pass a string instead of a char.

    Also note, you need to do the same for your CPU settings as well.

  26. #7316
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  27. #7317
    It's good to be free...
    Points: 10.420, Level: 67
    Level completed: 93%, Points required for next Level: 30
    Overall activity: 0%

    Registriert seit
    Feb 2007
    Beiträge
    2.440
    Points
    10.420
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    Zitat Zitat von eldiablov
    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?
    If you're using SEEK_SET and you start counting from 0, yes.
    pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ

  28. #7318
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    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...)

  29. #7319
    QJ Gamer Silver
    Points: 10.263, Level: 67
    Level completed: 54%, Points required for next Level: 187
    Overall activity: 0%

    Registriert seit
    Jun 2006
    Ort
    UK
    Beiträge
    2.326
    Points
    10.263
    Level
    67
    Downloads
    0
    Uploads
    0

    Standard

    http://www.cppreference.com/stdio/fputc.html

    That is why. Look at the parameter order.

    Code:
    fputc('1', options);

  30. #7320
    QJ Gamer Silver
    Points: 10.921, Level: 69
    Level completed: 18%, Points required for next Level: 329
    Overall activity: 0%

    Registriert seit
    May 2006
    Ort
    Behind you.
    Beiträge
    1.814
    Points
    10.921
    Level
    69
    Downloads
    0
    Uploads
    0

    Standard

    I got it:
    fputc('1', options);

    Edit: Same time you posted i posted too (lol...):
    http://www.mkssoftware.com/docs/man3/fputc.3.asp
    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...)


 

Tags for this Thread

Forumregeln

  • Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
  • Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
  • Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
  • Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
  •  





Alle Zeitangaben in WEZ -8. Es ist jetzt 09:14 PM Uhr.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © , Caputo Media, LLC. All Rights Reserved. Cluster .