试图删除空白在C code错误 [英] Error in a C code while trying to remove whitespace

查看:96
本文介绍了试图删除空白在C code错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此code是词法分析器的基础上,和它从源文件中删除空格的基本操作,并将其改写到与单独的行的每个字的另一个文件。
但我无法理解为什么文件lext.txt不会得到更新?

 #包括LT&;&stdio.h中GT;/ *这是它承认常量,变量,符号,标识,功能,评论也头文件的词法分析器。它存储在3个不同的文件的语意。一个文件包含了所有的标题和评论。另一个文件将包含所有的变量,另外一个将包含所有的符号。 * /诠释的main()
{
    INT I;
    烧焦A,B [20],C;
    FILE * FP1,FP2 *;    FP1 = FOPEN(的Source.txt,R); //源文件是只读将通过词法分析器传递模式打开
    FP2 =的fopen(lext.txt,W);
    //现在让我们去掉所有的空格和存储的话,其余的在一个文件    如果(FP1 == NULL)
    {
        PERROR(无法打开的Source.txt);
        //返回EXIT_FAILURE;
    }
    I = 0;
    而(!的feof(FP1))
    {
        A =龟etc(FP1);        如果(一个!=)
        {
            B〔I] = A;
        的printf(你好);
        }
        其他
        {            B〔I] ='\\ 0';
            fprintf中(FP2,%.20s的\\ n,B);
            I = 0;
            继续;
        }
        I = I + 1;        / *开关(一)
        {
            案例EOF:返回EOF;
            案例'+':符号=符号+ 1;            案例' - ':符号=符号+ 1;            案例'*':符号=符号+ 1;            案例'/':符号=符号+ 1;            案例'%':符号=符号+ 1;            案件 '
        * /
    }
返回0;
}


解决方案

只有当这种情况发生故障,您写入文件:

 如果(A =!)//不正确comparison..warning:指针和整数之间的比较

,从来没有这样做,你从来不去其他部分。

测试更改为:

 如果(一个!='')

编辑:

当你到达文件的结尾你刚刚终止程序,而无需编写什么是阵列中的 B 。因此,你需要另一个 fprintf中

 } // while循环结束。
B〔I] ='\\ 0';
fprintf中(FP2,%.20s的\\ n,B);

好吧,这里有云测试的code这就是和正常工作:)

 而(1)
{
    A =龟etc(FP1);
    如果(的feof(FP1))
        打破;    如果(一个!='')
        B〔我++] = A;
    其他{
        B〔I] ='\\ 0';
        fprintf中(FP2,%.20s的\\ n,B);
        I = 0;
    }
}
B〔I] ='\\ 0';
fprintf中(FP2,%.20s的\\ n,B);

this code is the base of lexer , and it does the basic operation of removing the whitespaces from a source file and rewrites it into another file with each word in separate lines . But i am not able to understand why the file lext.txt not getting updated?

#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,*fp2;

    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer
    fp2=fopen("lext.txt","w");  
    //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;
        printf("hello");
        }
        else
        {

            b[i]='\0';
            fprintf(fp2, "%.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;
}

解决方案

You write to the file only when this condition fails:

if(a!="") // incorrect comparison..warning: comparison between pointer and integer

which never does so you never go to the else part.

Change the test to:

if(a!=' ')

EDIT:

When you reach the end of the file you just terminate the program without writing what is in the array b. So you need another fprintf after the while loop:

} // end of while loop.
b[i]='\0';
fprintf(fp2, "%.20s\n", b);

Alright here goes the code thats tested and works fine :)

while(1)
{
    a=fgetc(fp1);
    if(feof(fp1))
        break;

    if(a!=' ')
        b[i++]=a;
    else {    
        b[i]='\0';
        fprintf(fp2, "%.20s\n", b);
        i=0;
    }    
}
b[i]='\0';
fprintf(fp2, "%.20s\n", b);

这篇关于试图删除空白在C code错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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