![]() |
|
#1
|
||||
|
||||
reading a char* into struct dataI'm reading data from a file and want to take that and put it into a struct, however, since the data isn't ALWAYS going to be coming from a file, I want to have this code take a char* (that could come from any source) and return my struct.
basically, I've got this: Code:
what would be the best way of doing that? I can't see looking at each byte and moving it to the appropriate variable as being the optimal way of doing that. the block of data that I'm reading is ALWAYS the same size and the data has a set amount of space (zero-padded) for gameName, if that helps. should I have the max-size explicitly defined when I declare gameID and makerCode and gameName? any ideas? Thanks. |
|
#2
|
||||
|
||||
|
Was the data put into the file as a struct in the first place? If so, you can put it into your struct as long as you know the size of the block you're getting out of the file.
http://www.gidforums.com/showpost.ph...3&postcount=12 __________________
-Aaron |
|
#3
|
||||
|
||||
|
Quote:
No, I'm not sure exactly how it was put in the file. And there are also some areas where there are extra padding zeros. I considered trying to build a struct in such a way that I could simply cast the char* as the struct, but it didn't seem like it would work out properly, and would be quite awkward. I guess my main goal would be to be able to fetch blocks of the data? like a substring-type function, but for memory (non-null-terminated)? |
|
#4
|
||||
|
||||
|
Hi Spike. I think that you may be able to typecast this. You would want to experiment a bit with it, but if you always have the same data block in the same order you should be able to type cast it. I would pass it as a void* though. Also, change your struct to be static, not dynamic:
CPP / C++ / C Code:
Like I said I would test this out, but you should be able to cram a block of memory into this thing. Cheers, d |
|
#5
|
||||
|
||||
|
if I typecast it, what should I do about the zero padding? (there's a couple sections that are either unknown or simply zero-padding...
Also, since there are u32s, what about endian-ness? |
|
#6
|
||||
|
||||
|
Quote:
Quote:
Yeah, that could be an issue. But if you could get everything else in line, you could just handle the two u32s seperately. You could try jamming these explicitly: CPP / C++ / C Code:
I don't know, you should do some testing on this defintely before you do too much coding. |
|
#7
|
||||
|
||||
|
well, with regard to the zero-padding, what I mean is that there are some unknown values between the fields in the struct. For instance, there's 6 zeros between the streamBufSize and the gameName.
Whether those are unknowns or just padding, I dunno. Would you suggest throwing a char unknown1[6]; in there? i was thinking about doing that from the beginning, but I just wasn't sure, it didn't seem right. do you think I should just code up a byteStream set of functions for handling this, so I can grab X characters starting at position Y, or grab a u32 from there, or whatnot? thanks. |
|
#8
|
||||
|
||||
|
Quote:
You know, by the time that you look at all of the special handling items this may be easier in the long run. You could just make a function, wherein you pass the data buffer and the malloc'ed structure and it will fill in the structure based upon the ruleset that you know. At first I was thinking that a typecast would be a pretty cool way to handle this, but now with all of the special handling I think you would be better off with something like this. I still would make your strings as static arrays if you definetely know the size of them though. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Urgent ! Pls Help Me ! | mycashmoney | C Programming Language | 4 | 01-Jul-2006 23:49 |
| Help with struct IORB | cIdiot | C Programming Language | 1 | 19-Apr-2004 09:09 |
| struct | fj8283888 | C Programming Language | 2 | 15-Apr-2004 13:31 |
| some I/O problems...again | cameron | C++ Forum | 3 | 03-Mar-2004 22:39 |
| Automate a data change php form | mjfmn | MySQL / PHP Forum | 4 | 20-Oct-2003 10:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The