(C代码) 我做了一个AVL树,它在netbeans上工作(用gcc编译器插件) 当我在linux终端中运行SAME代码(用gcc编译)时,我得到了一个“分段错误(核心转储) - 错误,这怎么可能?”
将数据从文本文件加载到结构中时会出现错误。
Node* load(Node* T, char* fileName) {
FILE* ptFile;
char myString[200];
char* ape1 = NULL; //data to insert on the node structure
char* ape2 = NULL;
char* nomb = NULL;
char* mail = NULL;
char* ciudad = NULL;
char* pais = NULL;
int fono = 0;
ptFile = fopen(fileName, "r");
if (ptFile == NULL) {
printf("ERROR file missing\n");
exit(1);
}
fgets(myString, 200, ptFile);
while (feof(ptFile) == 0) { // IF I COMMENT THIS LOOP THE ERROR DISAPEARS...BUT IM NOT GETTING THE DATA LOADED OBVIOUSLY
//HERE IS WHERE THE ERROR APPEARS
ape1 = strdup(strtok(myString, ",\n"));
if(myString != NULL){
ape2 = strdup(strtok(NULL, ",\n"));
nomb = strdup(strtok(NULL, ",\n"));
mail = strdup(strtok(NULL, ",\n"));
fono = atoi(strtok(NULL, ",\n"));
ciudad = strdup(strtok(NULL, ",\n"));
pais = strdup(strtok(NULL, ",\n"));
T = insert(T, ape1, ape2, nomb, mail, fono, ciudad, pais);
}
fgets(myString, 200, ptFile);
}
fclose(ptFile);
return T;
}
这个功能在netbenas上工作得很好,为什么我在终端上运行它时不能正常工作? 我做错了什么?...当我手动插入de数据时,插入功能正常工作。 感谢。
答案 0 :(得分:1)
试试这个
ape1 = strdup(strtok(myString, ",\n"));
if(myString != NULL){//always TRUE
更改为
char *temp = strtok(myString, ",\n");
if(temp != NULL){
ape1 = strdup(temp);
....