I'm novice so be gentle.
trying to read a file of strings as such: "1,Duck_Soup,1933,Comedy,5,12" and tokenize it to different vars for each of the tokens between the commas.
That's my code and I keep getting "segmentation fault" no matter what I try. Please help me fix the code, thank you. For starters I want to make it print the token separately, at least so I'll know it's working.
#include <stdio.h>
int main(int argc, char* argv[]) {
int i;
FILE *fd;
char fileName[40];
char line[100];
char *pch;
strcpy(fileName,argv[1]);
if((fd = fopen(fileName, "r")) == NULL) {
printf("Error opening file. \n");
return -1;
}
while( fgets(line, sizeof(line), fd) != NULL) {
pch = strtok (line, ",");
while (pch != NULL ) {
printf("%s", pch);
pch = strtok (NULL, ",");
}
}
fclose(fd);
}