从文件中读取数据,同时忽略非字母字符并存储要插入到trie中的变量

时间:2014-10-11 22:01:40

标签: c

我基本上是在尝试从文件中读取数据而忽略所有非字母字符,并且非字母前面的任何字符都将被视为单词的结尾,并应插入到trie中。有任何想法吗? 下面是我当前的代码,它正在获得一个seg错误

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>

 char input;

void readDict(FILE *dict_file)
{
    int line = 0;
    char curr;

    while ( (curr = fgetc(dict_file))!= EOF)
    {
        if(isalpha(curr))
        {
            strcpy(input,curr);
        }
    }
}

int main(int argc, char * argv[])
{
    if (argc < 2)
            printf("invalid input");
    else
    {
        FILE *pFile = fopen(argv[1],"r");
        readDict(pFile);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

分段错误由strcpy生成。您必须将char *传递给strcpy作为参数。在您发布的代码中,inputcurr都属于char类型。

阅读http://www.cplusplus.com/reference/cstring/strcpy/以了解如何使用strcpy