I want the program to read from an input file input.txt, identify each word, and save each word as a string in an array, named A.
I have tried following the other suggestions to no avail, my current code can print out each word in an array, but seems to save the word to every item in an array, instead of just one.
Any help would be much appreciated.
Sample input file:
Well this is a nice day.
Current output:
Well
this
is
a
nice
day.
This output should be the first word, not the last: day.
My code:
#include <stdio.h>
const char * readInputFile(char inputUrl[]) {
char inputLine[200];
char inputBuffer[200];
const char * A[1000];
int i = 0;
FILE *file = fopen(inputUrl, "r");
if(!file) {
printf("Error: cannot open input file.");
return NULL;
}
while(!feof(file)) {
fscanf(file,"%[^ \n\t\r]",inputLine); // Get input text
A[i] = inputLine;
fscanf(file,"%[ \n\t\r]",inputBuffer); // Remove any white space
printf("%s\n", A[i]);
i++;
}
printf("This output should be the first word, not the last: %s\n", A[0]);
fclose(file);
}
int main() {
readInputFile("input.txt");
return(0);
}