I have a text file following this format:
Thing 1: 0 0 128
Other thing: 255 64 255
Something else: 32 32 8
I intend to add more to this file eventually but the format will remain the same. What I want to do is read everything before the colon into a string and everything after it as an integer. I've tried this:
fscanf((file = fopen("colors.txt", "r")) == NULL){
return -1;
}
fscanf("%s: %d %d %d", colorStr, &r, &g, &b);
while(!feof(file)){
printf("%s: %d %d %d", colorStr, r, g, b);
fscanf(file, "%s: %d %d %d", colorStr, &r, &g, &b);
}
fclose(file);
However, I get this output:
Thing 1:: 0 0 0
0: 0 0 0
0: 0 0 0
128: 0 0 0
And so on. Ideally, the output should read like this:
Thing 1: 0 0 128
Other thing: 255 64 255
Something else: 32 32 8
How can I fix this? The colorStr
, r
, g
, and b
variables were set up earlier in the program.