fscanf(file, "%s %d", word, &value);
This gets the values โโdirectly into a string and an integer, and copes with variations in white space and number formats, etc.
Edit
Oh, I forgot that there are spaces between the words. In this case, I would do the following. (Note that it truncates the source text in the line)
// Scan to find the last space in the line char *p = line; char *lastSpace = null; while(*p != '\0') { if (*p == ' ') lastSpace = p; p++; } if (lastSpace == null) return("parse error"); // Replace the last space in the line with a NUL *lastSpace = '\0'; // Advance past the NUL to the first character of the number field lastSpace++; char *word = text; int number = atoi(lastSpace);
You can solve this with the stdlib functions, but the above will probably be more efficient, since you are only looking for the characters you are interested in.
Jason williams
source share