|
-
April 25th, 2008, 11:03 AM
#1
Reading memory stack
Hi gurus!!
I need to read a bin file, I have the specifications of the head of the file:
Byte 0 int
Byte 4 int
Byte 12 double
.....
Byte 99 double
I want to read the complite head (100 bytes) and pass this memory block to a class constructor who interpretate the values and inicializate the class which the apropiate values.
How can I do this.
-
April 25th, 2008, 11:12 AM
#2
Re: Reading memory stack
To get the raw data? fread() or istream::read, depending on whether you prefer <cstdio> or <iostream>.
To interpret the data? Mostly casting is fine, but if endian-ness is a potential problem (if this data file may be used by programs on multiple platforms), you may need to do some byte-swapping.
Incidentally, people will think you're talking about something else if you refer to this as a "memory stack".
-
April 26th, 2008, 03:40 AM
#3
Re: Reading memory stack
Thanks Lindley!!
In the especification of the file tell that file are in little endian byte order and later for each case especify if byte order is "big" or "little". Because of this I know when it is necesary to swapping the bytes.
Now appears other ask to me:
how I can swapping bytes.
For example (left less byte weight and right upper byte weight):
BYTE 0 TYPE integer(4 bytes) BYTE ORDER Big
BYTE 4 TYPE integer(4 bytes) BYTE ORDER Little
If byte order is "Big" and type is "integer" I understand that I need to read the four bytes in order "right to left".
If byte order is "Little" and type is "integer" I understand that I need to read the four bytes in order "left to right".
I am in the correct way?
EDIT:
Perhaps....
http://www.codeproject.com/KB/cpp/endianness.aspx
Code:
#include <algorithm> //required for std::swap
#define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))
void ByteSwap(unsigned char * b, int n)
{
register int i = 0;
register int j = n-1;
while (i<j)
{
std::swap(b[i], b[j]);
i++, j--;
}
}
I think it can help me.
Last edited by juanpast; April 26th, 2008 at 04:19 AM.
-
April 26th, 2008, 01:47 PM
#4
Re: Reading memory stack
It also depends on what the native endianness of the machine you're on is. You'll have to check that somehow.
For bytes stored as big-endian in the file, you can use ntohs() or ntohl()----these are network functions, since network byte order is big-endian. However, obviously these won't work for files containing little-endian information.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|