c中文件操作中的分段错误

时间:2010-04-18 14:18:06

标签: c segmentation-fault

#include<stdio.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
    int i;
    char a,b[20],c;
    FILE *fp1;

    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer

    //now lets remove all the white spaces and store the rest of the words in a file 

    if(fp1==NULL)
    {
        perror("failed to open source.txt");
        //return EXIT_FAILURE;
    }
    i=0;
    while(1)
    {


        a=fgetc(fp1);

        if(a !="")
        {
            b[i]=a;
        }
        else
        {


            fprintf(fp1, "%.20s\n", b);
            i=0;
            continue;
        }
        i=i+1;                  

        /*Switch(a)
        {
            case EOF :return eof;
            case '+':sym=sym+1;

            case '-':sym=sym+1;

            case '*':sym=sym+1;

            case '/':sym=sym+1;

            case '%':sym=sym+1;

            case '
        */
    }
return 0;
}

这段代码如何以分段错误结束?

5 个答案:

答案 0 :(得分:2)

而且,字符串b可能不会以空值终止。在您的代码中的某个时刻,您需要:

 b[i] = '\0';

答案 1 :(得分:2)

看起来需要检查以确保它不会超出数组b的末尾。如果它读取超过20个字符,它将写入并破坏堆栈。

答案 2 :(得分:0)

没有检查来测试End of File

更改

while(1) 

while (!feof(fp1))

修改

但是,seg-fault的原因是NULL末尾缺少b个字符。在将NULL字符写入文件之前,您可以在b的末尾添加b[i] = 0; // i must <=19 fprintf(fp1, "%.20s\n", b); 字符:

19

通过这种方式,您还需要确保不要将b char多于NULL,这样您总是有空间来编写{{1}}字符。

答案 3 :(得分:-1)

像这样使用你的时间:

...
 while((a=fgetc(fp1))!=EOF)
    {
        if(a !="")
        {
            b[i]=a;
        }
...

如果a与EOF不同,您将每次检查。如果a等于EOF,则处理将停止。

我会考虑使用fread() / fgets()读取更大的缓冲区,并解析生成的字符串,而不是逐个字符地读取。我认为这可能会提高解析器的整体性能。

答案 4 :(得分:-1)

段错误?通常是INFINITE LOOP ......


  

使用以下内容......

#include<stdio.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
    int i;
    char a,b[20],c;
    FILE *fp1;

    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer

    //now lets remove all the white spaces and store the rest of the words in a file 

    if(fp1==NULL)
    {
        perror("failed to open source.txt");
        //return EXIT_FAILURE;
    }
    i=0;
    while(!feof(fp1))
    {


        a=fgetc(fp1);

        if(a !="")
        {
            b[i]=a;
        }
        else
        {

            b[i]='\0';
            fprintf(fp1, "%.20s\n", b);
            i=0;
            continue;
        }
        i=i+1;                  

        /*Switch(a)
        {
            case EOF :return eof;
            case '+':sym=sym+1;

            case '-':sym=sym+1;

            case '*':sym=sym+1;

            case '/':sym=sym+1;

            case '%':sym=sym+1;

            case '
        */
    }
return 0;
}

古德纳克!!

CVS @ 2600Hertz