(C 代码) 我制作了一个AVL树,它在netbeans上工作(带有gcc编译器插件) 当我在 Linux 终端中运行相同的代码(也用 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 上运行得很好,为什么当我在终端上运行它时它不起作用? 我做错了什么?...当我手动插入数据时,插入功能工作正常。 谢谢。
最佳答案
试试这个
ape1 = strdup(strtok(myString, ",\n"));
if(myString != NULL){//always TRUE
更改为
char *temp = strtok(myString, ",\n");
if(temp != NULL){
ape1 = strdup(temp);
....
关于c - 相同的代码 (C) 无法在终端上运行,但可以在 IDE 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24464652/