Lingyun Yang wrote:
*** post for FREE via your newsreader at post.newsfeed.com ***
Dear all,
I have a file it's binary data viewed in UltraEdit is
EF BB BF 0D 0A 3C .......
I want to read them into a int or long int array byte[]
for example:
byte[0]=0xEFBB
byte[1]=0xBF0D
I write the following code, but the output isn't right:
/* ---------------code --------------- */
char dictfilename[256]="test.txt";
FILE *dictfile;
struct stat stats;
dictfile = fopen(dictfilename,"rb");
if (stat (dictfilename, &stats) == -1)
{
printf("dict file not exist!\n");
return 0;
}
int buffer[256]={1,2,3,4,5,6,7,8,9};
fread (buffer, sizeof(int), 256, dictfile);
fclose (dictfile);
for(int i=0; i<16; ++i)
printf("0xd%",buffer[i]);
/* ---------- end of the code ---------- */
the output is :
0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd
Is my code right?
No. Hell, it isn't even compilable. And even if you turn it into a
program, stat() is not a standard function. Here's some code for you to
play with:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *inout;
const unsigned char outdata[] =
{ 0xef, 0xbb, 0xbf, 0x0d, 0x0a, 0x3c };
/* Any of the following may lead to a zero-size array, which is not
legal C. I could do the checks at runtime, but I have left the
possibly illegal code, with a chicken-ass runtime check. */
unsigned short xshrt[sizeof outdata / sizeof(short)];
unsigned long xlong[sizeof outdata / sizeof(long)];
unsigned long long xllng[sizeof outdata / sizeof(long long)];
size_t i, Transfered;
if (!(inout = fopen("testdata", "wb"))) {
printf("could not open 'testdata' for outout\n");
exit(EXIT_FAILURE);
}
Transfered =
fwrite(outdata, sizeof *outdata,
sizeof outdata / sizeof *outdata, inout);
printf("Output transfer was %lu units of %lu bytes (wanted %lu)\n",
(unsigned long) Transfered, (unsigned long) sizeof *outdata,
(unsigned long) (sizeof outdata / sizeof *outdata));
fclose(inout);
if (sizeof outdata != sizeof xshrt) {
printf("There is a problem with reading into shorts.\n");
exit(EXIT_FAILURE);
}
if (!(inout = fopen("testdata", "rb"))) {
printf("could not open 'testdata' for outout\n");
exit(EXIT_FAILURE);
}
Transfered =
fread(xshrt, sizeof *xshrt,
sizeof xshrt / sizeof *xshrt, inout);
printf("Input transfer was %lu units of %lu bytes (wanted %lu)\n",
(unsigned short) Transfered, (unsigned short) sizeof *xshrt,
(unsigned short) (sizeof xshrt / sizeof *xshrt));
fclose(inout);
printf("Unsigned shorts: ");
for (i = 0; i < sizeof xshrt / sizeof *xshrt; i++)
printf("%#hx ", (unsigned short) xshrt[i]);
printf("\n\n");
if (sizeof outdata != sizeof xlong) {
printf("There is a problem with reading into longs.\n");
exit(EXIT_FAILURE);
}
if (!(inout = fopen("testdata", "rb"))) {
printf("could not open 'testdata' for outout\n");
exit(EXIT_FAILURE);
}
Transfered =
fread(xlong, sizeof *xlong,
sizeof xlong / sizeof *xlong, inout);
printf("Input transfer was %lu units of %lu bytes (wanted %lu)\n",
(unsigned long) Transfered, (unsigned long) sizeof *xlong,
(unsigned long) (sizeof xlong / sizeof *xlong));
fclose(inout);
printf("Unsigned shorts: ");
for (i = 0; i < sizeof xlong / sizeof *xlong; i++)
printf("%#lx ", xlong[i]);
printf("\n\n");
if (sizeof outdata != sizeof xllng) {
printf("There is a problem with reading into long longs.\n");
exit(EXIT_FAILURE);
}
if (!(inout = fopen("testdata", "rb"))) {
printf("could not open 'testdata' for outout\n");
exit(EXIT_FAILURE);
}
Transfered =
fread(xllng, sizeof *xllng,
sizeof xllng / sizeof *xllng, inout);
printf("Input transfer was %lu units of %lu bytes (wanted %lu)\n",
(unsigned long long) Transfered,
(unsigned long long) sizeof *xllng,
(unsigned long long) (sizeof xllng / sizeof *xllng));
fclose(inout);
printf("Unsigned shorts: ");
for (i = 0; i < sizeof xllng / sizeof *xllng; i++)
printf("%#llx ", xllng[i]);
printf("\n\n");
return 0;
}
[output]
Output transfer was 6 units of 1 bytes (wanted 6)
Input transfer was 3 units of 2 bytes (wanted 3)
Unsigned shorts: 0xbbef 0xdbf 0x3c0a
There is a problem with reading into longs.
--
Martin Ambuhl