You're getting close. Here's a minor adaptation — an MCVE (Minimal, Complete, Verifiable Example) that works:
#include <stdio.h>
int main(void)
{
int offset;
char line[1000];
FILE *fp = stdin; // Important but simple change
char term_in[1000];
while (fgets(line, sizeof(line), fp) != NULL) {
char *data = line;
while (sscanf(data, "%s%n", term_in, &offset) == 1) { // Changes
data += offset;
printf("[%s]\n", term_in); // Trivial change
}
}
return 0;
}
Here's an example run (it was called rl59.c
and compiled to rl59
):
$ rl59 <<< 'Once upon a time, there was a country that held nice, civilized presidential elections.'
[Once]
[upon]
[a]
[time,]
[there]
[was]
[a]
[country]
[that]
[held]
[nice,]
[civilized]
[presidential]
[elections.]
$
And given a multi-line input file:
So she went into the garden
to cut a cabbage-leaf
to make an apple-pie
and at the same time
a great she-bear coming down the street
pops its head into the shop
What no soap
So he died
and she very imprudently married the Barber
and there were present
the Picninnies
and the Joblillies
and the Garyulies
and the great Panjandrum himself
with the little round button at top
and they all fell to playing the game of catch-as-catch-can
till the gunpowder ran out at the heels of their boots
The output was:
[So]
[she]
[went]
[into]
[the]
[garden]
[to]
[cut]
[a]
[cabbage-leaf]
[to]
[make]
[an]
[apple-pie]
[and]
[at]
[the]
[same]
[time]
[a]
[great]
[she-bear]
[coming]
[down]
[the]
[street]
[pops]
[its]
[head]
[into]
[the]
[shop]
[What]
[no]
[soap]
[So]
[he]
[died]
[and]
[she]
[very]
[imprudently]
[married]
[the]
[Barber]
[and]
[there]
[were]
[present]
[the]
[Picninnies]
[and]
[the]
[Joblillies]
[and]
[the]
[Garyulies]
[and]
[the]
[great]
[Panjandrum]
[himself]
[with]
[the]
[little]
[round]
[button]
[at]
[top]
[and]
[they]
[all]
[fell]
[to]
[playing]
[the]
[game]
[of]
[catch-as-catch-can]
[till]
[the]
[gunpowder]
[ran]
[out]
[at]
[the]
[heels]
[of]
[their]
[boots]