read int(long) from a file

Discussion in 'C Programming' started by Lingyun Yang, Jan 25, 2004.

  1. Lingyun Yang

    Lingyun Yang Guest

    *** 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);

    /* ---------- end of the code ---------- */

    the output is :
    0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd0xd

    Is my code right?
    It seems fread didn't read anything into buffer[]

    Thank you for your help!


    Lingyun Yang




    -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
    http://www.newsfeed.com - The #1 Newsgroup Service in the World!
    -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
     
    Lingyun Yang, Jan 25, 2004
    #1
    1. Advertising

  2. 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);
    >
    > /* ---------- 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);
    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);
    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);
    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
     
    Martin Ambuhl, Jan 25, 2004
    #2
    1. Advertising

  3. Martin Ambuhl wrote [code that had many typos and errors]

    Please ignore that code, except, possibly, to fix it. I have sent a
    cancel, but those are largely ignored since the days of the rogue cancelbots.



    --
    Martin Ambuhl
     
    Martin Ambuhl, Jan 25, 2004
    #3
  4. 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);


    This is very bad. You need to read your C-book more carefully:

    {
    printf ("0x%X ", (unsigned) buffer);
    }
    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, 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);
    }
    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/
     
    Emmanuel Delahaye, Jan 25, 2004
    #4
  5. Lingyun Yang

    Lingyun Yang Guest

    *** post for FREE via your newsreader at post.newsfeed.com ***

    Thank you for your help.
    I am a newbie in C.
    as you said I should open my C-book and study carefully


    "Emmanuel Delahaye" <> дÈëÓʼþ
    news:Xns947B7C03C65A4hsnoservernet@213.228.0.75...
    > 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);

    >
    > This is very bad. You need to read your C-book more carefully:
    >
    > {
    > printf ("0x%X ", (unsigned) buffer);
    > }
    > 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, 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);
    > }
    > 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/





    -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
    http://www.newsfeed.com - The #1 Newsgroup Service in the World!
    -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
     
    Lingyun Yang, Jan 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,236
    Martien Verbruggen
    Jun 27, 2003
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,220
    Peter Shaggy Haywood
    Sep 20, 2005
  3. pereges

    Promoting unsigned long int to long int

    pereges, Jun 30, 2008, in forum: C Programming
    Replies:
    112
    Views:
    2,118
    David Thompson
    Jul 28, 2008
  4. veryhotsausage
    Replies:
    1
    Views:
    1,844
    veryhotsausage
    Jul 4, 2008
  5. Oliver Graeser
    Replies:
    10
    Views:
    595
    Oliver Graeser
    Sep 26, 2008
Loading...

Share This Page