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

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

问题描述

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

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," !\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~\n\t");
    while (word != NULL)
    {
        get_word(word); // get each word
        word = strtok (NULL, " !\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~\n\t");
    }
}
else
{
    fp = fopen( argv[2], "r" );
    if ( fp == 0 )
    {
        printf( "Could not open file\n" );
    }

           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\n" );
            }
            while (readFile(fp, word, size)) {
                    get_word(word);
            }
    }

    // do some stuff, sorting etc.

    fclose(fp);

FCLOSE(FP)不管它是否被打开了。如果 FP 没有连接到一个有效的流,分段错误是常见的。显然,一些实现处理 NULL 参数 FCLOSE 摆好,而这正是它似乎在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 调入其他分公司,并在的fopen 失败,不只是

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

printf( "Could not open file\n" );

但结束程序。

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

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