我正面臨問題分段錯誤。我知道這與記憶有關。我做了一些更改,但它始終存在錯誤。我正在嘗試讀取一個 25kb 的大檔案。我需要讀取一個最大的 csv 并將其逐行放入 struct 中。我是用鏈表做的。我也已經嘗試過使用串列。如果我在不拆分字串的情況下讀取檔案,我就沒有錯誤。
#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;
}
uj5u.com熱心網友回復:
如果沒有資料,很難說它到底在哪里崩潰,但是讓我們嘗試修復我可以在不運行代碼的情況下看到的問題:
#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;
}
您的代碼示例沒有使用鏈表,所以我省略了它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/378672.html
上一篇:為什么gcc使用__builtin_unreachable發出更糟糕的代碼?
下一篇:為什么這些運算式不適用于此代碼?
