segmentation fault in file operations in c

Posted by mekasperasky on Stack Overflow See other posts from Stack Overflow or by mekasperasky
Published on 2010-04-18T14:18:05Z Indexed on 2010/04/18 14:23 UTC
Read the original article Hit count: 228

Filed under:
#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;
}

how does this code end up in segmentation fault?

© Stack Overflow or respective owner

Related posts about c