?
Solved

Part 2: Read from a binary file

Posted on 2003-03-19
9
Medium Priority
?
226 Views
Last Modified: 2010-04-01
Hello,

I have a binary file whose structure is thus:
Identifier : [4 bytes]
RecordSize : [4 bytes]
Name : [null terminated string]
Address : [null terminated string]
Balance : [4 bytes]
..
..

Thanks to others, I am able to sucecssfully process the null terminated strings now.  But I want to be able to copy the Identifer, RecordSize and Balance from my memory buffer and into a local variable.

I was trying memcpy, but it doesn't seem to work (because I'm stuffing up something).

The Balance field is numerical in the database that created the original data file.  When I examine the data file with a Hex editor, I can see the value of this field (in Hex).  However, it is stored as a little endian.  Will I have to write code to accomodate the little endian storage, or will it be handled auto-magically for me?

I am starting off with a big project to learn my way through c++ - so please excuse any basic learner questions.

Thanks
0
Comment
Question by:Learner_dwarf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 8170014
>>Will I have to write code to accomodate the little
>>endian storage, or will it be handled auto-magically for
>>me?

It's not handle automatically, however, if the platform you're compiling to support little endian, then this shouldn't be an issue.

What is your OS and what compiler are you using?

I recommend that you post your code.
0
 
LVL 30

Expert Comment

by:Axter
ID: 8170053
You can use the following functions for little endian conversion:

htonl
ntohl

htons
ntohs
0
 

Expert Comment

by:arjkane
ID: 8170714
Hopefully those htonl and ntohl calls are available on your architecture.  You shouldn't need the memcpy call though:

char* buffer;
int x = *(int*)&buffer[4];

The variable x should then equal the integer interpretation of the 5th through 8th bytes of buffer...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Learner_dwarf
ID: 8171004
Arjkane

Thank you for your example.  How would that work if one of the variables was a char[4]?  
0
 

Expert Comment

by:arjkane
ID: 8171039
char buffer[4];
int x = *(int*)&buffer[0];

Should probably work.  Arrays ([]) are interchangable with pointers (*) in C/C++.
0
 

Author Comment

by:Learner_dwarf
ID: 8171140
Sorry, I meant my buffer is say 2000 chars long (char Buffer[2000]).

Part of this buffer contains say 10 characters I want to copy out into a char variable.

So, based on your previous example, how would I do that?


char buffer[2000];
char x= *(char*)&buffer[x];

(example above does not work)
0
 

Accepted Solution

by:
arjkane earned 400 total points
ID: 8171181
> Part of this buffer contains say 10 characters I want to copy out into a char variable.

I'm guessing you mean the buffer[2000] contains a 10 character string starting at x...

If the 10 character string is null terminated (ends with a '\0' char), then do this:

char* value = &buffer[x];

Note: value and buffer are both pointing at the same memory, if you want to get rid of buffer then do a strcpy().

If the 10 character string is not null terminated then do this:

char value[11];
value[10] = '\0';
memcpy( value, &buffer[x], 10 );
0
 

Author Comment

by:Learner_dwarf
ID: 8171193
Sorry, I meant my buffer is say 2000 chars long (char Buffer[2000]).

Part of this buffer contains say 10 characters I want to copy out into a char variable.

So, based on your previous example, how would I do that?


char buffer[2000];
char x= *(char*)&buffer[x];

(example above does not work)
0
 

Expert Comment

by:arjkane
ID: 8171196
> Part of this buffer contains say 10 characters I want to copy out into a char variable.

I'm guessing you mean the buffer[2000] contains a 10 character string starting at x...

If the 10 character string is null terminated (ends with a '\0' char), then do this:

char* value = &buffer[x];

Note: value and buffer are both pointing at the same memory, if you want to get rid of buffer then do a strcpy().

If the 10 character string is not null terminated then do this:

char value[11];
value[10] = '\0';
memcpy( value, &buffer[x], 10 );
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question