You could use scanf
to scan two words, then use fgets
to get the rest of the line.
FILE *f = stdin; // or use fopen to open a saved file
// use buffers large enough for your needs
char word1[20];
char word2[20];
char restOfLine[100];
// make sure fscanf returns 2 (indicating 2 items scanned successfully)
fscanf(f, "%20s %20s", word1, word2);
// make sure fgets returns &restOfLine[0]
fgets(restOfLine, sizeof restOfLine, f);
Note that if fgets
encounters a '\n'
character, it is put into the buffer as well, so if you don't want it, you'll have to strip it out manually. If fscanf
returns something other than 2, or fgets
returns NULL
, then there was a problem reading your input.