D
Default User
I'm need to do some parsing of a line from a config file. The data
structure is:
typedef struct
{
unsigned int version;
unsigned char timestamp[8];
unsigned int num_clients;
unsigned int num_msgs;
unsigned int num_channels;
unsigned int num_minor_frames;
float minor_frame_time;
float ex_capacity;
} M1553_Config_File_T;
An example string from the file would be:
"001 2005022718023400 1 3 2 4 0.05 0.001";
Most of it isn't a problem reading via sscanf(), except for the
timestamp.
2005022718023400 is a time stamp of the form YYYYMMDDHHMMSSTZ. I'm
trying to store it in that array in the struct, such that:
timestamp[0] == 20;
timestamp[1] == 5;
timestamp[2] == 2;
timestamp[3] == 27;
timestamp[4] == 18;
timestamp[5] == 2;
timestamp[6] == 34;
timestamp[7] == 0;
Right now I'm scanning the timestamp values into a temp array of
unsigned int with a bunch of %02u sequences, then copying all those
values to the array in the struct. Works, but a bit messy.
unsigned int timestamp[8];
stat = sscanf(str,
"%u%02u%02u%02u%02u%02u%02u%02u%02u%u%u%u%u%f%f",
&(config->version),
&(timestamp[0]),
&(timestamp[1]),
&(timestamp[2]),
&(timestamp[3]),
&(timestamp[4]),
&(timestamp[5]),
&(timestamp[6]),
&(timestamp[7]),
&(config->num_clients),
&(config->num_msgs),
&(config->num_channels),
&(config->num_minor_frames),
&(config->minor_frame_time),
&(config->ex_capacity)) == NUM_HEADER_FIELDS ? 0 : -1;
#ifdef DEBUG
if (stat == -1)
{
printf("read_header_record: Error interpreting %s\n",
str);
}
#endif
for (i=0; i < 8; i++)
config->timestamp = timestamp;
I'd like to do it directly via sscanf() but haven't come up with a scan
sequence. Some of you guys are hot-shot scanf() wizzes. Any ideas on a
magic (but safe and portable) way to do this?
Brian
structure is:
typedef struct
{
unsigned int version;
unsigned char timestamp[8];
unsigned int num_clients;
unsigned int num_msgs;
unsigned int num_channels;
unsigned int num_minor_frames;
float minor_frame_time;
float ex_capacity;
} M1553_Config_File_T;
An example string from the file would be:
"001 2005022718023400 1 3 2 4 0.05 0.001";
Most of it isn't a problem reading via sscanf(), except for the
timestamp.
2005022718023400 is a time stamp of the form YYYYMMDDHHMMSSTZ. I'm
trying to store it in that array in the struct, such that:
timestamp[0] == 20;
timestamp[1] == 5;
timestamp[2] == 2;
timestamp[3] == 27;
timestamp[4] == 18;
timestamp[5] == 2;
timestamp[6] == 34;
timestamp[7] == 0;
Right now I'm scanning the timestamp values into a temp array of
unsigned int with a bunch of %02u sequences, then copying all those
values to the array in the struct. Works, but a bit messy.
unsigned int timestamp[8];
stat = sscanf(str,
"%u%02u%02u%02u%02u%02u%02u%02u%02u%u%u%u%u%f%f",
&(config->version),
&(timestamp[0]),
&(timestamp[1]),
&(timestamp[2]),
&(timestamp[3]),
&(timestamp[4]),
&(timestamp[5]),
&(timestamp[6]),
&(timestamp[7]),
&(config->num_clients),
&(config->num_msgs),
&(config->num_channels),
&(config->num_minor_frames),
&(config->minor_frame_time),
&(config->ex_capacity)) == NUM_HEADER_FIELDS ? 0 : -1;
#ifdef DEBUG
if (stat == -1)
{
printf("read_header_record: Error interpreting %s\n",
str);
}
#endif
for (i=0; i < 8; i++)
config->timestamp = timestamp;
I'd like to do it directly via sscanf() but haven't come up with a scan
sequence. Some of you guys are hot-shot scanf() wizzes. Any ideas on a
magic (but safe and portable) way to do this?
Brian