I understand this is a basic question… but I have been stuck on it for an hours. I’m kind of new to C. I have been trying to parse in some integers from a textfile thats located in a particular fashion. Here is an example:
A 123,1
B 456,2
N 980,2
I want to throw out the letters, and the number after the commas. Hence, I would have just 123, 456, and 980. I have been getting stuck on the first part (throwing out the letters, and the white space in between has been screwing with me 🙁 ). I know there have been numerous posts that are somewhat similar, but I can’t seem to be able to get it. Here is my code so far (test.txt contains the input values).
int main(void)
{
FILE *rFile = fopen ("test.txt", "rt");
char input[20];
if (rFile == NULL) {
printf("Input file could not be located");
return -1;
}
while (fscanf(rFile, "%s \n", input) > 0)
{
printf("%s", input);
}
fclose(rFile);
return 0;
}
This is obviously not complete. My way about solving this problem is to take the input string (including the white spaces) and then use the strtok to cut out the middle part. However, I can’t even parse in each line of the textfile properly.
scanfandfscanfcan also parse single characters and integers. It ignores whitespace before entities.