Reputation: 195
I want to read the following input into scanf:
10 0 y
2
2, 80, 40, 0
This should be ready by scanf from a separate file.
I can get the first two lines without the commas by just doing the following:
scanf("%d %d %c %d", a1, a2, a3, c1, a4);
How would I be able to read the third line that is separated by commas and store those in my variables?
Upvotes: 2
Views: 6763
Reputation: 225042
You can just include the commas in a scanf
format string:
int blinky, pinky, inky, clyde;
scanf("%d, %d, %d, %d", &blinky, &pinky, &inky, &clyde);
Here's a complete example based on your question:
#include <stdio.h>
int main(void)
{
int a1, a2, a4;
int blinky, pinky, inky, clyde;
char c3;
scanf("%d %d %c %d %d, %d, %d, %d", &a1, &a2, &c3, &a4, &blinky, &pinky, &inky, &clyde);
printf("%d %d %c\n%d\n%d, %d, %d, %d\n", a1, a2, c3, a4, blinky, pinky, inky, clyde);
return 0;
}
And to build and test:
$ cat input
10 0 y
2
2, 80, 40, 0
$ make example
cc example.c -o example
$ ./example < input
10 0 y
2
2, 80, 40, 0
Upvotes: 8