If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Memcpy Manipulation into a Struct

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!

Re: Memcpy Manipulation into a Struct

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.

Re: Memcpy Manipulation into a Struct

Originally Posted by Draznar

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.

Re: Memcpy Manipulation into a Struct

Couple of suggestions.....

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.