473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Reading Binary file

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

Jul 14 '06 #1
8 2686

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

Jul 14 '06 #2
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
Jul 14 '06 #3
"Shalaka Joshi" <Js******@gmail.comwrites:
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...)
Jul 14 '06 #4
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" <Js******@gmail.comwrites:
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...)
Jul 14 '06 #5
On 2006-07-14, Shalaka Joshi <Js******@gmail.comwrote:
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
Jul 14 '06 #6
On 2006-07-14, Shalaka Joshi <Js******@gmail.comwrote:
Nelu wrote:
>"Shalaka Joshi" <Js******@gmail.comwrites:
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
Jul 14 '06 #7

"Shalaka Joshi" <Js******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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" <Js******@gmail.comwrites:
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

Jul 14 '06 #8
"Fred Kleinschmidt" <fr******************@boeing.comwrites:
[...]
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) ks***@mib.org <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.
Jul 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.