Thread: Basic file input code asplode (fread issues?)

  1. #1
    Registered User
    Join Date
    Dec 2006

    Basic file input code asplode (fread issues?)

    So this code pops up a message box and the message box says: "4294934642"
    That is NOT a short. A short is two bytes with a max of 65535.
    The code returns the correct value up until StringPointers[699]. StringPointers[698] is the value it's supposed to be.
    StringPointers[715] = 4294934736
    If anyone wants map.string I can email it to them.
    Finally, how the hell does 4294934642 fit in char DebugString[6]???

    Overly commented for my convenience due to me being a C newbie.
    If anyone finds any misconceptions in my comments PLEASE point them out. I'm learning C from my 14 year old brother (and internet tutorials somewhat), so I want to unlearn the bad bits I've learned as quickly as possible.
    Fortunately I'm getting K&R for Christmas

    int OpenTehFile() {
    	TehFile = fopen("map.string", "rb");
    	if(TehFile == NULL) {
    		return 0;
    	// Get file size
    	fseek(TehFile, 0, SEEK_END);
    	long FileSize = ftell(TehFile);
    	// Move to beginning of STR body
    	fseek(TehFile, 8, SEEK_SET);
    	// Get the short that tells SC the total of strings in the section, usually 1024
    	short StringTotal;
    	fread(&StringTotal, 2, 1, TehFile);
    	// Allocate enough memory to hold an array of all the string pointers,
    	// read the string pointers into the array
    	short * StringPointers;
    	// StringPointers is a pointer to a short
    	StringPointers = (short*)malloc(StringTotal*2);
    	// StringPointers is now pointing to a usually 2048 memory chunk
    	fread(StringPointers, StringTotal, 2, TehFile);
    	// Read 1024 2 byte chunks into StringPointers,
    	// but because StringPointers is a pointer to a 2048 memory address
    	// really read those chunks into the 2048 memory address
    	// The commented stuff here is debugging functions
    	char DebugString[6];
    	MessageBox(NULL, DebugString, "MESSAGE", MB_OK);
    Last edited by scwizard; 12-17-2006 at 10:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 4294934642
    Which in hex is FFFF8072

    > sprintf(DebugString,"%u",StringPointers[699]);
    sprintf is a variadic function (one which ends with ... in the prototype).
    This means everything after the format string follows default promotion rules (anything shorter than an int is promoted to int for example).
    Now since your short is a signed short, it gets promoted to a signed int, which is where all those FFFF come from, it's the sign bit of your short being propagated.
    If you really want to print a short, then use "%hu" in the format.

    > Finally, how the hell does 4294934642 fit in char DebugString[6]???
    It doesn't, but since sprintf() doesn't know how big the space is (you need snprintf for that), it goes off and trashes something else instead.

    Also, there is a lot of mixing of declarations and statements, which isn't part of normal C.
    You need C99 or C++ for that.
    Checking the return values of all the file functions would be a good idea as well.

    Do you have trouble spelling 'The'?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Do you have trouble spelling 'The'?
    FACT: People name variables funny things (until they learn better, which I most certainly haven't)

    Thanks it works now.

    Not only that, but it lead to change
    short * StringPointers;
    unsigned short * StringPointers;
    which fixed a very annoying bug.
    Last edited by scwizard; 12-17-2006 at 11:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
Website Security Test