从标准输入读取的分段错误(核心转储) [英] Segmentation fault (core dumped) read from stdin

查看:19
本文介绍了从标准输入读取的分段错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算文件中每个单词的数量.该文件可以是标准输入,也可以是命令行上提供的文件名 (./count -f ).到目前为止,当从命令行读取文件时,程序给出了正确的输出.但是当我尝试从标准输入读取时会发生错误.该程序首先输出正确,然后给出分段错误(核心转储).这是我的代码的一部分.

I am trying to count the number of each word in a file. The file can be either stdin or a filename provided on the command line(./count -f ). So far the program gives the correct outputs when reading a file from command line. But an error happens when i am trying to read from stdin. The program first output the correct, then give a Segmentation fault (core dumped). Here is part of my code.

    FILE * fp;
int size = 20000;
char sentence[2000]; // the sentence from stdin
if ( argc != 3 ) 
{
    fgets(sentence,sizeof(sentence),stdin); // read from stdin
    fflush(stdin);
    // I think the initialization of word is incorrect, but i do know why it is incorrect
    char *word = strtok (sentence," !"#$%&'()*+,./:;<=>?@[\]^_`{|}~
	");
    while (word != NULL)
    {
        get_word(word); // get each word
        word = strtok (NULL, " !"#$%&'()*+,./:;<=>?@[\]^_`{|}~
	");
    }
}
else
{
    fp = fopen( argv[2], "r" );
    if ( fp == 0 )
    {
        printf( "Could not open file
" );
    }

           char word[1000];
    while (readFile(fp, word, size)) {  // let the program read the file
        get_word(word); // get each word. Works well.
    }
}

get_word 函数:

get_word function:

void get_word(char *word){
node *ptr = NULL;
node *last = NULL;

if(first == NULL){
    first = add_to_list(word); // add to linked list
    return;
}

ptr = first;
while(ptr != NULL){
    if(strcmp(word, ptr->str) == 0){
        ++ptr->freq;
        return;
    }
    last = ptr;            
    ptr = ptr->next;  
}
last->next = add_to_list(word); // add to linked list

}

请帮我弄清楚为什么会出现分段错误(核心转储).该程序可以在我的 Mac 上运行,但不能在 Linux 上运行.
提前致谢.

Please help me figure out why i get a segmentation fault(core dumped). The program works on my mac, but does not work on Linux.
Thanks in advance.

推荐答案

问题是

int main (int argc, char *argv[]) {
    FILE * fp;

    if ( argc != 3 )
    {
            fgets(sentence,sizeof(sentence),stdin);
            // and so on
    }
    else
    {
            fp = fopen( argv[2], "r" );
            if ( fp == 0 )
            {
                    printf( "Could not open file
" );
            }
            while (readFile(fp, word, size)) {
                    get_word(word);
            }
    }

    // do some stuff, sorting etc.

    fclose(fp);

fclose(fp)不管它是否被打开.如果 fp 未连接到有效流,则分段错误很常见.显然,一些实现会优雅地处理 fcloseNULL 参数,这就是它看起来可以在 Mac 上工作的原因.

that you fclose(fp) regardless of whether it was opened. If fp is not connected to a valid stream, a segmentation fault is common. Apparently some implementations handle a NULL argument to fclose gracefully, and that's what makes it appear to work on Mac.

fclose调用移入else分支,当fopen失败时,不要随便

Move the fclose call into the else branch, and when fopen fails, don't just

printf( "Could not open file
" );

但结束程序.

这篇关于从标准输入读取的分段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆