 <p>Here's an easy way to do it, using the fact that on the lines in question, if your file follows the same format then the numbers will always appear in the same character of the line. This is a little bit fragile, and it'd be better to make your program more robust, to cope with arbitrary amounts of whitespace, for instance, but I'll leave that as an exercise to you:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #define MAX_LEN 100 #define BUY_LEN 9 #define AWARD_LEN 11 int main(void) { FILE * infile = fopen("file.dat", "r"); if ( !infile ) { perror("couldn't open file"); return EXIT_FAILURE; } char buffer[MAX_LEN]; char * endptr; while ( fgets(buffer, MAX_LEN, infile) ) { if ( !strncmp(buffer, "BUY ITEM ", BUY_LEN ) ) { char * num_start = buffer + BUY_LEN; long item = strtol(num_start, &amp;endptr, 0); if ( endptr == num_start ) { fprintf(stderr, "Badly formed input line: %s\n", buffer); return EXIT_FAILURE; } printf("Bought item %ld\n", item); } else if ( !strncmp(buffer, "AWARD ITEM ", AWARD_LEN) ) { char * num_start = buffer + AWARD_LEN; long item = strtol(num_start, &amp;endptr, 0); if ( endptr == num_start ) { fprintf(stderr, "Badly formed input line: %s\n", buffer); return EXIT_FAILURE; } printf("Awarded item %ld\n", item); } } fclose(infile); return 0; } </code></pre> <p>Running this with the sample data file in your question, you get:</p> <pre><code>paul@local:~/src/sandbox$ ./extr Bought item 8 Bought item 10 Awarded item 7 Bought item 1 Bought item 3 Awarded item 9 Bought item 7 paul@local:~/src/sandbox$ </code></pre> <p>Incidentally, based on one of the suggestions in your question, you might want to check out the answers to the question <a href="http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong">"<code>while( !feof( file ) )</code>" is always wrong</a>.</p>