I'm facing the issue segmentation fault. I know that is related with memory. I did some changes but its keeps with error. I'm trying read a big file 25kb. I need read a biggest csv and slip it in struct by line. I did it with linked list. I already tried also with a list. If I read the file without split the the string I dont have error.
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
typedef struct gh_user *GH_USER;
void separador (char *lines, GH_USER k) {
k->id = strdup(strsep(&lines, ";"));
k->login = strdup(strsep(&lines, ";"));
k->type = strdup(strsep(&lines, ";"));
k->createdat = strdup(strsep(&lines, ";"));
k->followers = strdup(strsep(&lines, ";"));
k->followerlist = strdup(strsep(&lines, ";"));
k->following = strdup(strsep(&lines, ";"));
k->followinglist = strdup(strsep(&lines, ";"));
k->public_gists = strdup(strsep(&lines, ";"));
k->pub_repos = strdup(strsep(&lines, ";"));
free(lines);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char *lines = malloc(1024);
GH_USER k = malloc(sizeof(struct gh_user));
int fileSize = 0;
while (fgets(lines, 1024,usr)!=NULL)
{
if(lines != NULL)
{
separador (lines,k->next);
printf("%s", k->type);
}
}
free(k);
fclose(usr);
free(lines);
return contador;
}
int main()
{
conta_bots();
return 0;
}
CodePudding user response:
Without the data it is hard to say where exactly it crashed, but let's try to fix issues I can see without running the code:
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct gh_user {
char *id;
char *login;
char *type;
char *createdat;
char *followers;
char *followerlist;
char *following;
char *followinglist;
char *public_gists;
char *pub_repos;
struct gh_user *next;
};
char *n_strdup(char *in) {
return strdup(in ? in : "");
}
void separador (char *lines, struct gh_user *k) {
k->id = n_strdup(strsep(&lines, ";"));
k->login = n_strdup(strsep(&lines, ";"));
k->type = n_strdup(strsep(&lines, ";"));
k->createdat = n_strdup(strsep(&lines, ";"));
k->followers = n_strdup(strsep(&lines, ";"));
k->followerlist = n_strdup(strsep(&lines, ";"));
k->following = n_strdup(strsep(&lines, ";"));
k->followinglist = n_strdup(strsep(&lines, ";"));
k->public_gists = n_strdup(strsep(&lines, ";"));
k->pub_repos = n_strdup(strsep(&lines, ";"));
}
/*yes, you need to free all that*/
void gh_user_free(struct gh_user *ptr) {
if (!ptr) return;
free(ptr->id);
free(ptr->login);
free(ptr->type);
free(ptr->createdat);
free(ptr->followers);
free(ptr->followerlist);
free(ptr->following);
free(ptr->followinglist);
free(ptr->public_gists);
free(ptr->pub_repos);
free(ptr);
}
int conta_bots()
{
FILE *usr;
usr = fopen("../entrada/users.csv","rb");
int contador = 0;
char lines[1024];
while (fgets(lines, 1024,usr)!=NULL)
{
struct gh_user *k = calloc(1, sizeof(struct gh_user));
separador (lines,k);
printf("%s", k->type);
gh_user_free(k);
}
fclose(usr);
return contador; /*possibly that var was supposed to be updated in some way?*/
}
int main()
{
conta_bots();
return 0;
}
Your code sample makes no use of the linked list, so I omitted that.