Doing some project for school, I encountered the following problem:
sscanf
reads different value that the one expected.
I want to read something like this:
1 0 185336079 0 0 168231418 -256 0 255 1
2 0 185336079 -256 0 168231418 -256 0 255 2
3 0 185336079 -256 0 168231418 -256 0 255 3
4 0 185336079 0 0 0 0 0 255 4
This is the code i currently use:
FILE *fd = open_fd("/proc/firewall", "r");
while ((read = getline(&line, &len, fd)) != -1) {
sscanf(line, "%d %c %d %d %d %d %d %d %c %c\n", &num,
&rule_u.inbound_outbound,
&rule_u.source_ip,
&rule_u.source_netmask,
&rule_u.source_port,
&rule_u.destination_ip,
&rule_u.destination_netmask,
&rule_u.destination_port,
&rule_u.protocol,
&rule_u.action);
printf("scanf read rule action : %c\n", rule_u.action);
printf("sscanf whole line:\n%s\n",line);
convert_rule_from_u();
print_rule();
}
The output the above code generates is the following:
scanf read rule action : 5
sscanf whole line:
1 0 185336079 0 0 168231418 -256 0 255 1
scanf read rule action : 5
sscanf whole line:
2 0 185336079 -256 0 168231418 -256 0 255 2
scanf read rule action : 5
sscanf whole line:
3 0 185336079 -256 0 168231418 -256 0 255 3
scanf read rule action : 5
sscanf whole line:
4 0 185336079 0 0 0 0 0 255 4
The expected output should be the following:
scanf read rule action : 1
sscanf whole line:
1 0 185336079 0 0 168231418 -256 0 255 1
scanf read rule action : 2
sscanf whole line:
2 0 185336079 -256 0 168231418 -256 0 255 2
scanf read rule action : 3
sscanf whole line:
3 0 185336079 -256 0 168231418 -256 0 255 3
scanf read rule action : 4
sscanf whole line:
4 0 185336079 0 0 0 0 0 255 4
rule_u struct
typedef struct rule_struct_u {
unsigned char inbound_outbound;
unsigned int source_ip;
unsigned int source_netmask;
unsigned int source_port;
unsigned int destination_ip;
unsigned int destination_netmask;
unsigned int destination_port;
unsigned char protocol;
unsigned char action;
} rule_struct_u;
What am I doing wrong?