My program reads in data from a formatted data file and uses the data to fill in some structures with the correct data. There are several types of structures so I am trying to do this in a general fashion, rather than hundreds of a = b lines. If this is just not a good idea, please tell me and I can just abandon this approach. With each piece of data read in from the file I know how big the structure is supposed to be, and how big each field of data is. My Problem is that while a lot of the data seems to be filled out correctly in the structure, a lot of it seems like garbage or not what it is supposed to be. Constructive advice would be appreciated!
--Message Format
# Message Name
Message Number
Message Length
Field Size Min Max Default
Field Size Min Max Default
(Cont.)
--Sample of Example of file the am reading data in from.
# Player Update
5
220
4 1 8 1
4 1 512 1
2 1 128 1
2 0 1 0
8 0 100000 0
8 -90.0 90.0 0
8 -180.0 180.0 0
4 -1000 60000 0
.......
--Code From Project
//At this point we have already processed the message number and length
There are serialization libraries which might help you with this task or even give you some ideas. My vote goes as always to boost, check serialization there.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
I don't know how well overloading istream for the structure would mesh with this project.
The big reason I was attempting to do all this memcpy() manipulation was so I could lessen the lines of code. There are many more data and types of structs to be done. If doing this with memcpy and pointer math isn't going to work out cleanly, then I will just have to have many lines of copying normally like you suggest.
Well, one potential problem is if someone in the future adds a non-POD item to a structure. Then the memcpy method will fail.
Another is that compiler options change the alignment of data.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
I wish I could get it to work 100% and not 60% of the time, I don't have a good way to loop so doing it all row by row is going to look bloated :-\
I wish I could get it to work 100% and not 60% of the time, I don't have a good way to loop so doing it all row by row is going to look bloated :-\
Then memcpy is the worst thing you can do. As John already pointed out you will get into trouble when adding some non-POD members to you struct, resulting in undefined behaviour. Thatīs the worst you can get, because you really donīt know whatīs going on from then. UD is the hardest to track down, so take the advice given and overload the stream operators or use a serialization library.
"Plain Old Data" and "Undefined Behaviour", though the latter abbreviation is not actually used in the C++ Standard (and it should be UB, not UD, methinks).
Last edited by laserlight; February 18th, 2009 at 11:02 AM.
Reason: Added links, UD -> UB.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
1) You could look into auto-generating the IO routines. There are various ways to do that. For the most part they're more trouble than they're worth, though, until you start getting to the point where you have a hundred different structs with a hundred different fields each.
2) If you're worried about code looking bloated, hide all the struct manipulation routines away in a separate file. You can then write them once and forget about them.
"Plain Old Data" and "Undefined Behaviour", though the latter abbreviation is not actually used in the C++ Standard (and it should be UB, not UD, methinks).
Of course itīs UB, itīs a typo in my previous post.
Bookmarks