I'm currently trying to read a csv file using strsep but it never passes the first line
int main(){
FILE *fp = fopen("users100.csv", "r");
if(!fp){
printf("Erro");
return 0;
}
char *str;
str = (char *) malloc(10240);
while(fgets (str, 10240, fp) != NULL){
char *tmp = strdup(str);
char *token;
char **sp = &str;
sp = &tmp;
while(token = strsep(&str, ";")){
printf("%s ", token);
}
putchar('\n');
}
free(str);
fclose(fp);
return 0;
}
The output of this program is
public_repos id followers follower_list type following_list public_gists created_at following login
Segmentation fault (core dumped)
It prints the first line but not the rest.
Thank you!
The problem is that in this call
the pointer
stris changed.For starters there is no great sense to reinitialize the pointer sp.
You should write
In this while loop you need to write
And then you need to free the both strings