Advertisement Jump to content
Sign in to follow this  

fread resulting an extra "ffffff" in front of the actual value i have?

This topic is 4375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have some trouble with fread compiled with VC6 and VC 2005. [help] The codes are:
/* Read-in target file into memory */
int readin (char *fname, char** out)
    FILE*   fp = NULL;
    long    fsize = 0;
    /* check if the file can be open */
    if (!(fp = fopen(fname, "rb")))
        return -1;

    /* check file size */
    fseek (fp, 0, SEEK_END);
    fsize = ftell (fp);
    rewind (fp);

    /* allocate mem */
   *out = (char*) malloc (fsize);
    if (out == NULL)
        return -2;

    /* Read-in file */
    fread(*out, 4, fsize/4, fp);

    return fsize;

int main()
   char* p;
   int   fsize;

   fsize = readin (_some_fillname_, &p);


But after some certain bytes of char I read in, there are a few chars that have extra "ffffff" in front of the real value.
real value,     read-in value
94              ffffff94
0A              ffffff0A

So strange [dead], I just couldn't know what did i do wrong here... Thanks in advance [smile]

Share this post

Link to post
Share on other sites
Take a look at two's complement, most specifically this bit (no pun intended).

In a nutshell, when the most significant bit is set the number is assumed to be negative. When casting up, therefore, if the most significant bit is set then the new upper bits will also be set. Therefore, 0x00..0x7F will become 0x00000000..0x0000007F and 0x80..0xFF will become 0xFFFFFF80..0xFFFFFFFF when casted up to a 32-bit integer.

A simple fix might be to use unsigned char instead of char.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!