Code:
#include <stdio.h>
void getAllToken(char *line,char *tokenList[]){
char token[50];
char *ptr = line;
int i = 0;
/*while is not the end of line get the
token and store it into tokenList*/
while(ptr != '\0'){
ptr = getNextToken(ptr,token);
strcpy(token,tokenList[i]);
i++;
}
}
char *getNextToken(char *ptr,char *token){
/*skipping all the spaces*/
while((*ptr != '\0') && (*ptr == ' ')){
ptr++;
}
while((*ptr != '\0') && (*ptr != ' ')){
*token++ = *ptr++;
}
*token = '\0';
return ptr;
}
void interpret(char *line){
char tokenList[20][30];
getAllToken(line,tokenList);
printf("%s\n",tokenList);
}
int main(int argc,char *argv[]){
FILE *fp;
char line[1000];
if(argc != 2){
printf("Invalid number of files\n");
exit(1);
}
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("Invalid file");
}
/*get line by line from the file*/
while(!feof(fp)){
fgets(line,1000,fp);
interpret(line);
}
return 0;
}
this segment of code is suppose to extract all the words in a file and to be stored in an array (without using strtok)
the problem is i got segmentation fault for this piece of code and i am not too sure how to insert values into the array from another function