GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2004, 22:33
spike666's Avatar
spike666 spike666 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
spike666 is on a distinguished road

reading a char* into struct data


I'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:
typedef struct gcm_disk_header { char systemID; char *gameID; //2 bytes char regionCode; char *makerCode; //2 bytes u32 streamBufSize; //unsigned 32-bit integer char *gameName; //variable size... u32 fstOffset; }GCMDiskHeader;

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  
Old 18-Apr-2004, 23:07
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 19-Apr-2004, 00:27
spike666's Avatar
spike666 spike666 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
spike666 is on a distinguished road
Quote:
Originally Posted by aaroncohn
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.

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  
Old 19-Apr-2004, 09:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
typedef struct gcm_disk_header {
    char systemID;
    char gameID[2]; //2 bytes
    char regionCode;
    char makerCode[2]; //2 bytes
    u32 streamBufSize; //unsigned 32-bit integer
    char gameName[SIZE]; //variable size... - You said this could be static
    u32 fstOffset;
}GCMDiskHeader;


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  
Old 19-Apr-2004, 10:24
spike666's Avatar
spike666 spike666 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
spike666 is on a distinguished road
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  
Old 19-Apr-2004, 12:53
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by spike666
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...
Didn't you say that gameName would always be the same size? If you can make it a static size it shouldn't matter that I can think of. It will still be a null terminated string and the last u32 will start at the end of SIZE.

Quote:
Also, since there are u32s, what about endian-ness?

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:
current_header.streamBufSize = (u32) *(block+offset);

I don't know, you should do some testing on this defintely before you do too much coding.
  #7  
Old 19-Apr-2004, 13:01
spike666's Avatar
spike666 spike666 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
spike666 is on a distinguished road
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  
Old 19-Apr-2004, 13:06
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by spike666
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.

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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 03:14.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.