473,320 Members | 1,940 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,320 software developers and data experts.

Reading in and out of a character buffer

I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.

Sep 5 '06 #1
4 2962
n_******@hotmail.com wrote:
I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.
Yes. See these FAQs on serialization:

http://www.parashift.com/c++-faq-lit...alization.html

Also consider Boost.Serialization:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M

Sep 5 '06 #2
<n_******@hotmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.
"Simple casting doesn't seem to do the trick." What do you mean by this? I
do this type of thing all the time for various things, usually sending data
over the network.

If I have an int and a character pointer, treat the character pointer as an
int pointer and derefence it.

(Untested code)

unsigned char MyArray[100];
int MyInt = 20;

char* CharP = &MyArray[10];

*reinterpret_cast<int*>( CharP ) = MyInt;
or
*reinterpret_cast<int*>( &MyArray[10] ) = MyInt;

This would change bytes 10, 11, 12 and 13 of the char array to the binary
value located in MyInt. To load it back go the other way.

MyInt = *reinterpret_cast<int*>( &MyArray[10] );

If this gives you problems (it should work fine as is?) you may need an
extra set of parenthesis:

MyInt = *reinterpret_cast<int*>( &(MyArray[10]) );

but I don't think you do.
Sep 5 '06 #3
Great, thanks a lot!
Jim Langston wrote:
<n_******@hotmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.

"Simple casting doesn't seem to do the trick." What do you mean by this? I
do this type of thing all the time for various things, usually sending data
over the network.

If I have an int and a character pointer, treat the character pointer as an
int pointer and derefence it.

(Untested code)

unsigned char MyArray[100];
int MyInt = 20;

char* CharP = &MyArray[10];

*reinterpret_cast<int*>( CharP ) = MyInt;
or
*reinterpret_cast<int*>( &MyArray[10] ) = MyInt;

This would change bytes 10, 11, 12 and 13 of the char array to the binary
value located in MyInt. To load it back go the other way.

MyInt = *reinterpret_cast<int*>( &MyArray[10] );

If this gives you problems (it should work fine as is?) you may need an
extra set of parenthesis:

MyInt = *reinterpret_cast<int*>( &(MyArray[10]) );

but I don't think you do.
Sep 6 '06 #4
n_******@hotmail.com wrote:
Great, thanks a lot!

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>

Brian
Sep 6 '06 #5

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

Similar topics

11
by: mkarja | last post by:
Hi, I'm trying to figure out how to read some range of rows from a file. Is it possible to search the file with some criteria and then when the search string is found read 3 rows before and...
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
12
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Ha­rry,St....
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
28
by: Colin JN Breame | last post by:
Hi, Fairly new to C. What is the best way to read a line (\n terminated) from a file? Ive looked at fscanf but was not sure which format specifier to use. (%s perhaps). Thanks Colin
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
4
by: seema_coma | last post by:
I am working under a Linux environment, and I want to "read stream up to next delimiter". It looks like linux doesnt't have a function such as bgets() All i need is, reading characters from...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
7
by: Vlad Dogaru | last post by:
Hello, I suspect this comes up quite often, but I haven't found an exact solution in the FAQ. I have to read and parse a file with arbitrarily long lines and have come up with the following...
8
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should...
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
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.