M
Mark
I have a text file with values written in a format: <value
1-25><space><0xhex_value>. Tthese values represent a structure with two
fields:
typedef structure
{
uint32_t vid;
uint32_t member_port;
} vlanParam_t;
I need to extract the values, I though the following code would be
sufficient for that:
int save_cfg(vlanParam_t *cfg)
{
FILE *fp;
char name[] = "vlanXXXX.cfg";
snprintf(name, sizeof(name), "vlan%d.cfg", cfg->vid);
if ((fp = fopen(name, "w+")) == NULL)
return -1;
fprintf(fp, "%u 0x%04x\n", cfg->vid, cfg->memberPort);
}
int read_cfg(vlanParam_t *cfg)
{
FILE *fp;
char name[] = "vlanXXXX.cfg";
char buf[30];
char *token;
snprintf(name, sizeof(name), "vlan%d.cfg", cfg->vid);
if ((fp = fopen(name, "r")) == NULL)
return -1;
while (fgets(buf, sizeof(buf), fp) != NULL) {
for (token = strtok(buf, " "); token != NULL; token = strtok(NULL, "
")) {
printf("%d ", atoi(token));
}
putchar('\n');
}
}
But read_cfg() only prints first field, followed by one space (so far so
good) and the leading zero of a hex value. What am I doing wrong?
1-25><space><0xhex_value>. Tthese values represent a structure with two
fields:
typedef structure
{
uint32_t vid;
uint32_t member_port;
} vlanParam_t;
I need to extract the values, I though the following code would be
sufficient for that:
int save_cfg(vlanParam_t *cfg)
{
FILE *fp;
char name[] = "vlanXXXX.cfg";
snprintf(name, sizeof(name), "vlan%d.cfg", cfg->vid);
if ((fp = fopen(name, "w+")) == NULL)
return -1;
fprintf(fp, "%u 0x%04x\n", cfg->vid, cfg->memberPort);
}
int read_cfg(vlanParam_t *cfg)
{
FILE *fp;
char name[] = "vlanXXXX.cfg";
char buf[30];
char *token;
snprintf(name, sizeof(name), "vlan%d.cfg", cfg->vid);
if ((fp = fopen(name, "r")) == NULL)
return -1;
while (fgets(buf, sizeof(buf), fp) != NULL) {
for (token = strtok(buf, " "); token != NULL; token = strtok(NULL, "
")) {
printf("%d ", atoi(token));
}
putchar('\n');
}
}
But read_cfg() only prints first field, followed by one space (so far so
good) and the leading zero of a hex value. What am I doing wrong?