2

I have problem with reading a binary file in C, the problem is: when i read the file it will not read the complete file but left some of the data from last... the code used to read the file is as below:

            FILE *file;             
            unsigned long fileLen;

            //Open file
            file = fopen("ASELogo.png", "rb");
            if (!file)
            {
                fprintf(stderr, "Unable to open file %s", name);
            }               

            fseek(file, 0, SEEK_END);
            fileLen=ftell(file);
            fseek(file, 0, SEEK_SET);

            buffer=(char *)malloc((fileLen+1)*sizeof(unsigned char));

            fread(buffer, fileLen, 1, file);

            for(int i = 0;i < fileLen;++i)
                printf("%c", ((char *)buffer)[i]);

And when m printing the file it will not print the whole contents of the file...

the actual file contents are:

âPNG

2 Answers 2

3

You're printing out bytes as characters. That's fine if they happen to be ASCII range, but you're going to end up printing control characters and so on.

Try formatting the characters as bytes (e.g. 0xFF and 0x12) and see if that helps.

6
  • 2
    @Vinay change the "%c" in printf to "%x" and see if you get the entire output.
    – badgerr
    Jun 9, 2011 at 9:50
  • 1
    To get the gist of what I'm saying, replace the PNG file with a simple text file. I'll bet that'll work fine. Instead of printf("%c", buffer[i]), try using a hexadecimal format specifier ("%2x" maybe can't remember off the top of my head) and see if that works with the PNG file Jun 9, 2011 at 9:50
  • @Jelf: but the problem still there... i am trying to write the other png file using the buffer but...it fails...the code used to write te file as as:::<code>(test = fopen("test.png", "wb"));int n = fwrite(buffer, fileLen, 1, test);<code>
    – Vinay Jain
    Jun 9, 2011 at 9:52
  • 1
    So it looks like the code is correct, but the way you are printing things out is wrong. I'm not sure what you're expecting to see (certainly not the PNG I hope!), but printing out bytes as ASCII characters isn't a good idea. As badgerr suggested replace %c with %x and it'll "work" (in quotes because I don't have an idea what work means for you). Jun 9, 2011 at 10:14
  • 1
    I would guess that the way you are writing it is wrong. That's a different question to the one you've asked though. If you want to write binary data, use fwrite not fprintf. I suspect you are printing out characters to a file and not bytes. Jun 9, 2011 at 10:19
0

You don't mention how much you read, and how big is the file, but it's probably related to the fact that you compare signed int (i) to unsigned long (fileLen) if the file is big enough, you're gonna have troubles.

BTW, you allocate to char *, but uses size of unsigned char. why is that?

Edit

And one more thing: you don't check the return value of fread. Check if the expected amount of bytes have been read, and if not, check perror. See here.

1
  • I had tried to post the output but it fails..due to unsufficient rank...but i ahve made changes as u said: changed unsigned char to char and unsigned long to int but problem is still there...
    – Vinay Jain
    Jun 9, 2011 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.