Hi,

I need to read byte by byte data from header-like file. The format of the file is in the link, please take a look
http://imageshack.us/photo/my-images/96/tablefu.jpg/

I dont need the first 128bytes and I need to get the next 4 bytes. My code is like below:

    char* icm_name = "Samsung_to_Cannon.icm";
    char buf[5];

    FILE* fp = fopen(icm_name, "rb");
    fseek( fp, 128, SEEK_SET );
    fread( buf, 1, 4, fp );
    buf[4] = '\0';
    printf("%s\n",buf);

    fclose( fp );

But it returns nothing.. What is wrong with my code? Please give me some advices..

Thanks

Recommended Answers

All 7 Replies

check the return value of fopen() -- its probably failed to open the file. Does the file exist in the current working directory?

Yes file is there. If I do the same thing with a text file, it works fine. I guess it is because the format of data in ICM file is not ascii, utf or whatever. And I guess 'fopen' only reads text format of data so it returns reads nothing from header's binary data. Is there any other good function to read file?

Yes. fopen(). Your analysis of the problem is completely wrong. fopen() can open any file.

As AD said, "check the return value of fopen()". The fopen() probably failed for some reason.

when fopen opens a text file that contains "123456789abcde",
it prints "123456789abcde" well.

when fopen opens a icm file that shows broken texts in notepad,
it prints nothing.

if fopen reads the data from icm file properly, I think I need to do some data conversion such as binary to ascii. How do you think?

sometime when using

printf("%s\n",buf);

it prints junk and commands which maybe sometimes show nothing,

what I recomment

try the next code line instead

cout.write(buf, 4) << endl;

wow thanks, cout.write(buf, 4) prints the broken texts just like notepad. Which means it reads the file properly.
Now I guess I need to convert the data to whatever readable.. do you have an idae?

use

strtol
//search a reference, it should convery any str value to any format
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.