In 'comp.lang.c', "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[]
It makes a difference. Be more informative.
The id 'byte' is inappropriate here. A 'byte' is the smallest addressable
amount of memory for a given platform.
> for example:
> byte[0]=0xEFBB
> byte[1]=0xBF0D
> I write the following code, but the output isn't right:
Your code is very broken.
> /* ---------------code --------------- */
Assuming a wrapper such as:
#include <stdio.h>
int main (void)
{
....
return 0;
}
> char dictfilename[256]="test.txt";
Why do you waste so many bytes for a simple string literal?
static char const dictfilename[] = "test.txt";
> FILE *dictfile;
> struct stat stats;
This is not standard C.
> dictfile = fopen(dictfilename,"rb");
> if (stat (dictfilename, &stats) == -1)
The standard basic way is:
if (dictfile == NULL)
> {
> printf("dict file not exist!\n");
> return 0;
0 means 'OK'. Better to use EXIT_FAILURE...
> }
>
> int buffer[256]={1,2,3,4,5,6,7,8,9};
Why in the world do you need to initialize this array? Debug purpose?
> fread (buffer, sizeof(int), 256, dictfile);
It's important to store and test the returned value. It gives indications
about the reading operation results. Open your C-book for details.
> fclose (dictfile);
> for(int i=0; i<16; ++i)
> printf("0xd%",buffer[i]);
This is very bad. You need to read your C-book more carefully:
{
printf ("0x%X ", (unsigned) buffer[i]);
}
printf ("\n");
I see what you intend to do. Be careful that the binary representation of
objetcts bigger than a byte may change from an implementation to another. It
means that you file can have the following bytes :
0x12 0x34
but once read and converted 'rawly', you can obtain:
0x1234
0x0001234
0x3412
0x34120000
etc. which are all different. Details belongs to your platform (endianness,
data width etc.)
Nota that some implementations supply 'hton()' 'or 'ntoh()' family functions
to convert properly the data (assuming h = host and n = network, hence MSB
first)
> /* ---------- end of the code ---------- */
Try this, but be careful, as explained above, the result is not portable.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main (void)
{
int ret;
static char const dictfilename[] = "test.txt";
FILE *dictfile = fopen (dictfilename, "rb");
if (dictfile == NULL)
{
if (errno)
{
perror (dictfilename);
}
ret = EXIT_FAILURE;
#if 0
/* to make the binary file if not exists... */
{
char buffer[] =
{0xEF, 0xBB, 0xBF, 0x0D, 0x0A, 0x3C};
FILE *dictfile = fopen (dictfilename, "wb");
size_t i;
for (i = 0; i < sizeof buffer; i++)
{
fputc (buffer[i], dictfile);
}
fclose (dictfile);
}
#endif
}
else
{
int buffer[256] =
{1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = fread (buffer
,sizeof *buffer
,sizeof buffer / sizeof *buffer
,dictfile);
if (feof (dictfile))
{
printf ("EOF\n");
}
if (ferror (dictfile))
{
if (errno)
{
perror (dictfilename);
}
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
fclose (dictfile);
{
int i;
for (i = 0; i < n; ++i)
{
printf ("%04X ", (unsigned) buffer[i]);
}
printf ("\n");
}
}
return ret;
}
<Borland C 3.1 (16-bit)>
D:\CLC\Y\YANG>bc proj.prj
EOF
BBEF 0DBF 3C0A
</>
--
-ed-
[remove YOURBRA before answering me]
The C-language FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
C-reference:
http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
FAQ de f.c.l.c :
http://www.isty-info.uvsq.fr/~rumeau/fclc/