twitter
    Find out what I'm doing, Follow Me :)

Reading A Word At A Time

Load and display the file named readtext.c for an example of how to read a word at a time.


#include "/sys/stdio.h"
main( )
{
FILE *fp1;
char oneword[100];
int c;
fp1 = fopen("TENLINES.TXT","r");
do {
c = fscanf(fp1,"%s",oneword); /* got one word from the file */
printf("%s\n",oneword); /* display it on the monitor */
} while (c != EOF); /* repeat until EOF */
fclose(fp1);
}



This program is nearly identical as the last except that this program uses the "fscanf" function to read in a string at a time. Because the "fscanf" function stops reading when it finds a space or a newline character, it will read a word at a time, and display the results one word to a line.


You will see this when you compile and run it, but first we must examine a programming problem.