Reading Binary file

Discussion in 'C Programming' started by Shalaka Joshi, Jul 14, 2006.

  1. Hi,

    I have binary file say, "test.bin". I write "FF" in the file and expect
    my code to read 255 for me.

    char * lbuf;
    int lreadBytes ;
    long lData;

    FILE *pFile = fopen ("c:\\testbin","rb");

    // Read data from binary file
    lreadBytes = fread(lbuf,1 ,1,pFile);

    if(lreadBytes!= 0)
    {
    lData = atol(lbuf); // converting char* to long
    }

    This does not work. lData is not 255 as i expect
    How do I convert char* to long when reading the binary data?

    Thanks and Regards,
    Shalaka
    Shalaka Joshi, Jul 14, 2006
    #1
    1. Advertising

  2. Shalaka Joshi

    Tom St Denis Guest

    Shalaka Joshi wrote:
    > Hi,
    >
    > I have binary file say, "test.bin". I write "FF" in the file and expect
    > my code to read 255 for me.
    >
    > char * lbuf;
    > int lreadBytes ;
    > long lData;
    >
    > FILE *pFile = fopen ("c:\\testbin","rb");
    >
    > // Read data from binary file
    > lreadBytes = fread(lbuf,1 ,1,pFile);
    >
    > if(lreadBytes!= 0)
    > {
    > lData = atol(lbuf); // converting char* to long
    > }


    What do you think is in lbuf[0] at this point [provided it pointed
    somewhere valid]?

    Also where does lbuf point? This isn't a valid code snippet.

    What is actually in the file? The byte 0xFF or the ASCII characters
    FF?

    Tom
    Tom St Denis, Jul 14, 2006
    #2
    1. Advertising

  3. Hi,

    thanks for ur response..

    lbuf reads the value correctly and shows me the "ascii" code
    corresponding to "ff" .
    In file I have 0xFF and not the ASCII FF.

    Best Regards,
    Shal


    Tom St Denis wrote:
    > Shalaka Joshi wrote:
    > > Hi,
    > >
    > > I have binary file say, "test.bin". I write "FF" in the file and expect
    > > my code to read 255 for me.
    > >
    > > char * lbuf;
    > > int lreadBytes ;
    > > long lData;
    > >
    > > FILE *pFile = fopen ("c:\\testbin","rb");
    > >
    > > // Read data from binary file
    > > lreadBytes = fread(lbuf,1 ,1,pFile);
    > >
    > > if(lreadBytes!= 0)
    > > {
    > > lData = atol(lbuf); // converting char* to long
    > > }

    >
    > What do you think is in lbuf[0] at this point [provided it pointed
    > somewhere valid]?
    >
    > Also where does lbuf point? This isn't a valid code snippet.
    >
    > What is actually in the file? The byte 0xFF or the ASCII characters
    > FF?
    >
    > Tom
    Shalaka Joshi, Jul 14, 2006
    #3
  4. Shalaka Joshi

    Nelu Guest

    "Shalaka Joshi" <> writes:

    > Hi,
    >
    > I have binary file say, "test.bin". I write "FF" in the file and expect
    > my code to read 255 for me.


    If you write "FF" into the file why do you expect the value to be 255?
    It may be 255 if you write 0xff into the file. "FF" probably means two
    bytes, each one being equal to the character code of 'F'.

    >
    > char * lbuf;
    > int lreadBytes ;
    > long lData;
    >
    > FILE *pFile = fopen ("c:\\testbin","rb");
    >
    > // Read data from binary file
    > lreadBytes = fread(lbuf,1 ,1,pFile);


    You read into the memory location lbuf points to, but you haven't
    allocated lbuf so you may not be allowed to write there.
    If you read only one character and don't want to be bothered with
    malloc then I suggest you declare lbuf as:

    char lbuf

    and read into &lbuf.


    >
    > if(lreadBytes!= 0)
    > {
    > lData = atol(lbuf); // converting char* to long
    > }


    atol needs a string. What you provide (beside not being allocated) is
    also not-null terminated so it's not a string. Even if it was correct,
    atol returns the value of the number represented by the string, so, if
    the string is "34512", atol would return the long 34512. In your case
    F is not a long, and if you used 0xFF (which is a number not a string)
    then your value would be the (probably ASCII) character corresponding
    to 255 and not the string "255".


    >
    > This does not work. lData is not 255 as i expect
    > How do I convert char* to long when reading the binary data?


    Also, char is signed and 255 may well be out of its range (considering
    x86 with char between -127 and 127).

    It depends how you want to convert char * to long. You can convert the
    value of a pointer to an int and cast that value to long (probably not
    what you want) and, if char * refers to a string, then you can use
    atol (but you are messing things up above).

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
    Nelu, Jul 14, 2006
    #4
  5. Sorry this is just a snapshot of the code. I have allocated memory to
    lbuf.

    well, I have 0xff into the file. So the value is 255.

    I know atol is not working here, but my question is, I have a binary
    file which has 0xff in it. When I read the file it reads ascii code in
    lbuf (of type char*) corresponding to "0xff ".

    Now if I have a variable of type long which I want to assign the value
    255. How do I extract lbuf value into my variable?






    Nelu wrote:
    > "Shalaka Joshi" <> writes:
    >
    > > Hi,
    > >
    > > I have binary file say, "test.bin". I write "FF" in the file and expect
    > > my code to read 255 for me.

    >
    > If you write "FF" into the file why do you expect the value to be 255?
    > It may be 255 if you write 0xff into the file. "FF" probably means two
    > bytes, each one being equal to the character code of 'F'.
    >
    > >
    > > char * lbuf;
    > > int lreadBytes ;
    > > long lData;
    > >
    > > FILE *pFile = fopen ("c:\\testbin","rb");
    > >
    > > // Read data from binary file
    > > lreadBytes = fread(lbuf,1 ,1,pFile);

    >
    > You read into the memory location lbuf points to, but you haven't
    > allocated lbuf so you may not be allowed to write there.
    > If you read only one character and don't want to be bothered with
    > malloc then I suggest you declare lbuf as:
    >
    > char lbuf
    >
    > and read into &lbuf.
    >
    >
    > >
    > > if(lreadBytes!= 0)
    > > {
    > > lData = atol(lbuf); // converting char* to long
    > > }

    >
    > atol needs a string. What you provide (beside not being allocated) is
    > also not-null terminated so it's not a string. Even if it was correct,
    > atol returns the value of the number represented by the string, so, if
    > the string is "34512", atol would return the long 34512. In your case
    > F is not a long, and if you used 0xFF (which is a number not a string)
    > then your value would be the (probably ASCII) character corresponding
    > to 255 and not the string "255".
    >
    >
    > >
    > > This does not work. lData is not 255 as i expect
    > > How do I convert char* to long when reading the binary data?

    >
    > Also, char is signed and 255 may well be out of its range (considering
    > x86 with char between -127 and 127).
    >
    > It depends how you want to convert char * to long. You can convert the
    > value of a pointer to an int and cast that value to long (probably not
    > what you want) and, if char * refers to a string, then you can use
    > atol (but you are messing things up above).
    >
    > --
    > Ioan - Ciprian Tandau
    > tandau _at_ freeshell _dot_ org (hope it's not too late)
    > (... and that it still works...)
    Shalaka Joshi, Jul 14, 2006
    #5
  6. On 2006-07-14, Shalaka Joshi <> wrote:
    > Tom St Denis wrote:
    >> Shalaka Joshi wrote:
    >> > Hi,
    >> >
    >> > I have binary file say, "test.bin". I write "FF" in the file and expect
    >> > my code to read 255 for me.
    >> >
    >> > char * lbuf;
    >> > int lreadBytes ;
    >> > long lData;
    >> >
    >> > FILE *pFile = fopen ("c:\\testbin","rb");
    >> >
    >> > // Read data from binary file
    >> > lreadBytes = fread(lbuf,1 ,1,pFile);
    >> >
    >> > if(lreadBytes!= 0)
    >> > {
    >> > lData = atol(lbuf); // converting char* to long
    >> > }

    >>
    >> What do you think is in lbuf[0] at this point [provided it pointed
    >> somewhere valid]?
    >>
    >> Also where does lbuf point? This isn't a valid code snippet.
    >>
    >> What is actually in the file? The byte 0xFF or the ASCII characters
    >> FF?
    >>

    >
    > Hi,
    >
    > thanks for ur response..
    >
    > lbuf reads the value correctly and shows me the "ascii" code
    > corresponding to "ff" .
    > In file I have 0xFF and not the ASCII FF.
    >


    Don't top post. I've fixed it here.

    lbuf is most certainly not reading the value correctly, even though
    you may think that it is.

    You need to allocate memory before you use it.

    --
    Andrew Poelstra <http://www.wpsoftware.net/projects/>
    To email me, use "apoelstra" at the above domain.
    "You people hate mathematics." -- James Harris
    Andrew Poelstra, Jul 14, 2006
    #6
  7. On 2006-07-14, Shalaka Joshi <> wrote:
    > Nelu wrote:
    >> "Shalaka Joshi" <> writes:
    >>
    >> > Hi,
    >> >
    >> > I have binary file say, "test.bin". I write "FF" in the file and expect
    >> > my code to read 255 for me.

    >>
    >> If you write "FF" into the file why do you expect the value to be 255?
    >> It may be 255 if you write 0xff into the file. "FF" probably means two
    >> bytes, each one being equal to the character code of 'F'.
    >>
    >> >
    >> > char * lbuf;
    >> > int lreadBytes ;
    >> > long lData;
    >> >
    >> > FILE *pFile = fopen ("c:\\testbin","rb");
    >> >
    >> > // Read data from binary file
    >> > lreadBytes = fread(lbuf,1 ,1,pFile);

    >>
    >> You read into the memory location lbuf points to, but you haven't
    >> allocated lbuf so you may not be allowed to write there.
    >> If you read only one character and don't want to be bothered with
    >> malloc then I suggest you declare lbuf as:
    >>
    >> char lbuf
    >>
    >> and read into &lbuf.
    >>
    >>
    >> >
    >> > if(lreadBytes!= 0)
    >> > {
    >> > lData = atol(lbuf); // converting char* to long
    >> > }

    >>
    >> atol needs a string. What you provide (beside not being allocated) is
    >> also not-null terminated so it's not a string. Even if it was correct,
    >> atol returns the value of the number represented by the string, so, if
    >> the string is "34512", atol would return the long 34512. In your case
    >> F is not a long, and if you used 0xFF (which is a number not a string)
    >> then your value would be the (probably ASCII) character corresponding
    >> to 255 and not the string "255".
    >>
    >>
    >> >
    >> > This does not work. lData is not 255 as i expect
    >> > How do I convert char* to long when reading the binary data?

    >>
    >> Also, char is signed and 255 may well be out of its range (considering
    >> x86 with char between -127 and 127).
    >>
    >> It depends how you want to convert char * to long. You can convert the
    >> value of a pointer to an int and cast that value to long (probably not
    >> what you want) and, if char * refers to a string, then you can use
    >> atol (but you are messing things up above).
    >>

    >
    > Sorry this is just a snapshot of the code. I have allocated memory to
    > lbuf.
    >
    > well, I have 0xff into the file. So the value is 255.
    >
    > I know atol is not working here, but my question is, I have a binary
    > file which has 0xff in it. When I read the file it reads ascii code in
    > lbuf (of type char*) corresponding to "0xff ".
    >
    > Now if I have a variable of type long which I want to assign the value
    > 255. How do I extract lbuf value into my variable?
    >


    1) Stop topposting.
    2) Open your file in notepad or whatever your text editor is and paste
    it into your message. We would like to see the contents of this file.
    3) Allocate memory for lbuf. You've been told this twice by my count.


    --
    Andrew Poelstra <http://www.wpsoftware.net/projects/>
    To email me, use "apoelstra" at the above domain.
    "You people hate mathematics." -- James Harris
    Andrew Poelstra, Jul 14, 2006
    #7
  8. "Shalaka Joshi" <> wrote in message
    news:...
    > Sorry this is just a snapshot of the code. I have allocated memory to
    > lbuf.
    >
    > well, I have 0xff into the file. So the value is 255.
    >
    > I know atol is not working here, but my question is, I have a binary
    > file which has 0xff in it. When I read the file it reads ascii code in
    > lbuf (of type char*) corresponding to "0xff ".
    >
    > Now if I have a variable of type long which I want to assign the value
    > 255. How do I extract lbuf value into my variable?
    >
    >
    >
    >
    >
    >
    > Nelu wrote:
    >> "Shalaka Joshi" <> writes:
    >>
    >> > Hi,
    >> >
    >> > I have binary file say, "test.bin". I write "FF" in the file and expect
    >> > my code to read 255 for me.

    >>
    >> If you write "FF" into the file why do you expect the value to be 255?
    >> It may be 255 if you write 0xff into the file. "FF" probably means two
    >> bytes, each one being equal to the character code of 'F'.
    >>
    >> >
    >> > char * lbuf;
    >> > int lreadBytes ;
    >> > long lData;
    >> >
    >> > FILE *pFile = fopen ("c:\\testbin","rb");
    >> >
    >> > // Read data from binary file
    >> > lreadBytes = fread(lbuf,1 ,1,pFile);

    >>
    >> You read into the memory location lbuf points to, but you haven't
    >> allocated lbuf so you may not be allowed to write there.
    >> If you read only one character and don't want to be bothered with
    >> malloc then I suggest you declare lbuf as:
    >>
    >> char lbuf
    >>
    >> and read into &lbuf.
    >>
    >>
    >> >
    >> > if(lreadBytes!= 0)
    >> > {
    >> > lData = atol(lbuf); // converting char* to long
    >> > }

    >>
    >> atol needs a string. What you provide (beside not being allocated) is
    >> also not-null terminated so it's not a string. Even if it was correct,
    >> atol returns the value of the number represented by the string, so, if
    >> the string is "34512", atol would return the long 34512. In your case
    >> F is not a long, and if you used 0xFF (which is a number not a string)
    >> then your value would be the (probably ASCII) character corresponding
    >> to 255 and not the string "255".
    >>
    >>
    >> >
    >> > This does not work. lData is not 255 as i expect
    >> > How do I convert char* to long when reading the binary data?

    >>
    >> Also, char is signed and 255 may well be out of its range (considering
    >> x86 with char between -127 and 127).
    >>
    >> It depends how you want to convert char * to long. You can convert the
    >> value of a pointer to an int and cast that value to long (probably not
    >> what you want) and, if char * refers to a string, then you can use
    >> atol (but you are messing things up above).
    >>
    >> --
    >> Ioan - Ciprian Tandau
    >> tandau _at_ freeshell _dot_ org (hope it's not too late)
    >> (... and that it still works...)

    >

    Assuming you fix up the code so that it properly allocates the buffer, etc.,
    and you do read the contents in, you can get the integer value using a
    simple cast.
    For example:
    unsigned char c = 0xff;
    long i = (long)x;
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, Jul 14, 2006
    #8
  9. "Fred Kleinschmidt" <> writes:
    [...]
    > Assuming you fix up the code so that it properly allocates the buffer, etc.,
    > and you do read the contents in, you can get the integer value using a
    > simple cast.
    > For example:
    > unsigned char c = 0xff;
    > long i = (long)x;


    The cast isn't necessary:

    unsigned char c = 0xff;
    long i = x;

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 14, 2006
    #9
    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. Neo
    Replies:
    0
    Views:
    466
  2. El Durango

    reading binary file.

    El Durango, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    7,545
    Gordon Beaton
    Dec 8, 2004
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    914
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,640
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    723
Loading...

Share This Page