Within your while
loop instead of immediately printing the character that you read, store the char
in a char
array. Add an if
statement that does a comparison that checks if the read char
is a space character. If it is you should print the stored array and set the index of the array back to 0.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("file.txt","r");
char ch[100];
int index = 0;
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
//printf("%c\n", ch[index]);
if(ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your: %s\n", ch);
index = 0;
}
else {
index++;
}
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
17
solved How to read a file word by word? [closed]