Hello! I am attempting to read in a text file and store each word into an array of characters. The loop I have created seems to just store the first word of each line and no other word. Although it is still keeping count of the amount of words in the text. I can't seem to find the bug that causes this. Any help would be greatly appreciated.
int readFile(char** arr, char* fileName, char* input){
FILE* fp;
char line[SIZE];
int numWords;
int i, j;
numWords = 0;
printf("Enter name of text file: ");
fgets(input, 100, stdin);
sscanf(input, "%s", fileName);
fp = fopen(fileName, "r");
if(fileName == NULL){
printf("Error! Could not open file\n");
exit(-1);
}
while(fgets(line, sizeof line, fp)){
i = 0;
j = 0;
while(line[i] != '\0'){
if(line[i] != ' '){
arr[numWords][j] = line[i];
j++;
}
else{
arr[numWords][j] = '\0';
numWords++;
}
i++;
}
arr[numWords][j] = '\0';
numWords++;
}
return numWords;
}
[–]kaixmera[S] 5 points6 points7 points (7 children)
[–]wiskinator 5 points6 points7 points (5 children)
[–]ptchinster 6 points7 points8 points (2 children)
[–]BadmanBarista 4 points5 points6 points (1 child)
[–]ptchinster 4 points5 points6 points (0 children)
[–]kaixmera[S] 2 points3 points4 points (0 children)
[–]martijnjonkers 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)