is there a limit to char buffer sizes?

Discussion in 'Game Development (Technical)' started by cliffski, Aug 27, 2004.

  1. cliffski

    Moderator Original Member

    Joined:
    Jul 27, 2004
    Messages:
    3,897
    Likes Received:
    0
    ive got a weird bug where im using fgets (or fstream) to read a line of text from a file. im setting my buffer at 1024 chars, but if the input is over 512 chars, then it seems to 'overcopy' the amount, and basically miss the next new line. its very weird...

    #define MAXSTRING 1024

    FILE* fileptr = fopen("text.txt","rt");
    while(!feof(fileptr))
    {
    char buffer[MAXSTRING];
    strcpy(buffer,"");
    fgets(buffer,MAXSTRING,fileptr);
    }

    what am i missing?
     
  2. oNyx

    Original Member

    Joined:
    Jul 26, 2004
    Messages:
    1,212
    Likes Received:
    0
    IIRC gets() should return an int with the number of bytes which were actually read.
     
  3. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    Hmmm. My only doubt is reading MAXSTRING characters instead of MAXSTRING-1, because of the \0 added by fgets(). You could have problems if you happen to read a MAXSTRING characters line (writting MAXSTRING + 1 bytes to buffer)

    Other than that, it should work fine. Maybe there's an implementation-specific limitation? I find nothing in the man page.
     
  4. GameStudioD

    Original Member

    Joined:
    Jul 27, 2004
    Messages:
    154
    Likes Received:
    0
    The code looks ok, except for what ggambett pointed out. I have done strings bigger than 1024 before ... it should work. It sounds like fgets is missing an end of string char for some reason. Take a look at the input coming in, maybe a space or newline char is missing or the input is just messed up. Also, check ferror to see if an error occured.

    I think this might be an "I forgot to cross the t" error when you do figure it out.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer