tags:

views:

94

answers:

2

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;
}
+4  A: 

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);
codaddict
it still doesnt work .. if i put ab da in file sources.txt only ab comes to lext.txt
mekasperasky
Answer updated.
codaddict
Remove the continue. hmmm??...Complete-code below.
CVS-2600Hertz
A: 

You Forgot to FLUSH!! (among other things...) :-P


if(a!=' ')

and

//writes pending data to file

fflush();

and

//Releases file-pointer

fclose(fp2);

Here's the complete code:

#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;
        }
        i=i+1;

        //writes pending data to file
        fprintf(fp2, "%.20s\n", b);
        fflush();

        //Releases file-pointer
        fclose(fp2);   

        /*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;
}

GoodLUCK!!

CVS @ 2600Hertz

CVS-2600Hertz
Hmmm... a minor change, Done!!
CVS-2600Hertz
Nice observation Unicorn!!
CVS-2600Hertz