c - 相同的代码 (C) 无法在终端上运行,但可以在 IDE 上运行

标签 c gcc netbeans

(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/

相关文章:

linux - 遗留的 gcc 编译器问题

使用 flex 编译多个词法分析器会导致重新定义错误

java - 通过在 Netbeans 中添加 JPanel 来实现 JFrame 的可见性

java - 是否有可能加速 log4j 或 slf4j 输出(也可能全局关闭它们)?

java - Tomcat 无法在主机上下文 server.xml 中设置 'source' 属性集

c - Arduino millis不工作

c - 如果我不包含头文件会发生什么

c - 当输入是 int 和 string 的混合时,scanf 读取两次

c - gcc gtk2和CANbus合并编译

c - C 中的多重条件检查