[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Part 2: Read from a binary file

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
Learner_dwarf
Asked:
Learner_dwarf
  • 4
  • 3
  • 2
1 Solution
 
AxterCommented:
>>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
 
AxterCommented:
You can use the following functions for little endian conversion:

htonl
ntohl

htons
ntohs
0
 
arjkaneCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Learner_dwarfAuthor Commented:
Arjkane

Thank you for your example.  How would that work if one of the variables was a char[4]?  
0
 
arjkaneCommented:
char buffer[4];
int x = *(int*)&buffer[0];

Should probably work.  Arrays ([]) are interchangable with pointers (*) in C/C++.
0
 
Learner_dwarfAuthor Commented:
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
 
arjkaneCommented:
> 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
 
Learner_dwarfAuthor Commented:
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
 
arjkaneCommented:
> 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now