How to remove Segmentation Fault

This is a discussion on How to remove Segmentation Fault within the C Programming forums, part of the General Programming Boards category; The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    How to remove Segmentation Fault

    The following code can detect a (single) macro name (stored in str1) from a file and store its definition (stored in str2) and then reads the file again to write into a new file, replacing all occurrences of the macro name with its definition. The code when executed gives segmentation fault, please help me remove it. the mac() is working properly, there's a problem with the main(). Please help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    FILE *fp1, *fp2;
    int i;
    char str[10];
    char str1[10],str2[10];
    
    
    void mac (void) {
        char ch;
    
    
        fp1=fopen("comm.txt","r");
    
    
        do{
            ch=fgetc(fp1);
            if(ch!='\n')
            {
                while(ch!='\n')
                {
                    ch=fgetc(fp1);
                }
            }
            ch=fgetc(fp1);
            if (ch=='#')
            {
                ch=fgetc(fp1);
                if(ch==' ')
                    {
                    i=0;
                    ch=fgetc(fp1);
                    while (ch!=' ')
                        {
                        str1[i++]=ch;
                        ch=fgetc(fp1);
                        }
                    str1[i]='\0';
                    }
                //puts(str1);
                if(!(strcmp(str1,"define")))
                {
    
    
                    i=0;
                    ch=fgetc(fp1);
                    while(ch!=' ' && ch!='\n')
                    {
                        str2[i++]=ch;
                        ch=fgetc(fp1);
                    }
    
    
                    str2[i]='\0';
    
    
                }
    
    
            }
        } while(ch!= EOF);
    
    
        fclose(fp1);
    
    
    
    
    
    
    }
    
    
    int main(void)
    {
        char ch;
        int flag=1;
        fp1=fopen("comm.txt","r");
        fp2=fopen("new.txt","w");
        mac();
        do{
            if(flag==0)
                {
                    ch=fgetc(fp1);
                }
                
            if (ch!='#')
            {
                while(ch!='\n' && ch!=';')
    
    
                {
                    i=0;
                    while(ch!=' ' && ch!='\n' && ch!=';')
                    {
                    str[i++]=ch;
                    ch=fgetc(fp1);
                    }
    
    
                str[i]='\0';
                //ch=fgetc(fp1);
                if (!(strcmp(str,str1)))
                    fputs(str2,fp2);
                else
                    fputs(str,fp2);
                fputc(ch,fp1);
                ch=fgetc(fp1);
                }
                fputc(ch,fp1);
                if (ch=='\n')
                    flag=1;
                else
                    flag =0;
            }
            else
            {
            while(ch!='\n')
                    {
                    fputc(ch,fp2);
                    ch=fgetc(fp1);
    
    
                    }
            fputc(ch,fp2);
            flag=1;
            }
    
    
        }while(ch!=EOF);
        fclose(fp1);
        fclose(fp2);
    
    
    
    
        return EXIT_SUCCESS;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, you don't bother checking the return value of fopen. Could that be it?

    Oh, and you open "comm.txt" twice! Once in main and once in mac.

    And what's up with all the pointless globals?

    Also, if someone wanted to test your code, they would need the data files.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Segmentation fault is usually doing something with memory that is pointing to NULL

    Check to see if your files open before using them.

    fp1 is opened twice in main and mac, you shouldn't do that.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    mac() closes fp1 and then you try to use it in main - That could be it
    Fact - Beethoven wrote his first symphony in C

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    A solution to this would be if you declare local file pointers, not global - Or at least a local file pointer for your operations in mac()
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Okay thanks
    I declared the file pointers local, yet the compiler shows segmentation error.
    The 2 files are simply any arbitrary files with comm.txt being a c program file.
    Please help.

    The following is the edited code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int i;
    char str[10];
    char str1[10],str2[10];
    
    
    void mac (void) {
    	FILE *fp1;
    
    
    	char ch;
    
    
    	fp1=fopen("comm.txt","r");
    if (fp1==NULL)
    {
    printf("Cant open file!");
    exit(0);
    }
    	do{
    		ch=fgetc(fp1);
    		if(ch!='\n')
    		{
    			while(ch!='\n')
    			{
    				ch=fgetc(fp1);
    			}
    		}
    		ch=fgetc(fp1);
    		if (ch=='#')
    		{
    			ch=fgetc(fp1);
    			if(ch==' ')
    				{
    				i=0;
    				ch=fgetc(fp1);
    				while (ch!=' ')
    					{
    					str1[i++]=ch;
    					ch=fgetc(fp1);
    					}
    				str1[i]='\0';
    				}
    			//puts(str1);
    			if(!(strcmp(str1,"define")))
    			{
    
    
    				i=0;
    				ch=fgetc(fp1);
    				while(ch!=' ' && ch!='\n')
    				{
    					str2[i++]=ch;
    					ch=fgetc(fp1);
    				}
    
    
    				str2[i]='\0';
    
    
    			}
    
    
    		}
    	} while(ch!= EOF);
    
    
    	fclose(fp1);
    
    
    
    
    
    
    }
    
    
    int main(void)
    {
    	char ch;
    	FILE *fp1, *fp2;
    if (fp1==NULL)
    {
    printf("Cant open file!");
    exit(0);
    }
    
    
    	int flag=1;
    	fp1=fopen("comm.txt","r");
    	fp2=fopen("new.txt","w");
    	mac();
    	do{
    		if(flag==0)
    			{
    				ch=fgetc(fp1);
    			}
    			
    		if (ch!='#')
    		{
    			while(ch!='\n' && ch!=';')
    
    
    			{
    				i=0;
    				while(ch!=' ' && ch!='\n' && ch!=';')
    				{
    				str[i++]=ch;
    				ch=fgetc(fp1);
    				}
    
    
    			str[i]='\0';
    			//ch=fgetc(fp1);
    			if (!(strcmp(str,str1)))
    				fputs(str2,fp2);
    			else
    				fputs(str,fp2);
    			fputc(ch,fp1);
    			ch=fgetc(fp1);
    			}
    			fputc(ch,fp1);
    			if (ch=='\n')
    				flag=1;
    			else
    				flag =0;
    		}
    		else
    		{
    		while(ch!='\n')
    				{
    				fputc(ch,fp2);
    				ch=fgetc(fp1);
    
    
    				}
    		fputc(ch,fp2);
    		flag=1;
    		}
    
    
    	}while(ch!=EOF);
    	fclose(fp1);
    	fclose(fp2);
    
    
    
    
    	return EXIT_SUCCESS;
    }
    Last edited by yamini_16; 09-19-2012 at 03:48 AM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    #1.
    Code:
    void mac (void) {
        FILE *fp1;
     
     
        char ch;
    
        ...
    
        ch=fgetc(fp1);
    
        ...
    
        } while(ch!= EOF);
    The fgetc function returns an int, not a char. This is very important when comparing against EOF. The var ch should be declared as an int. You do this both in main and in mac.




    #2
    At the top of the do-while loop in function mac, all this:
    Code:
    ch=fgetc(fp1);
    if(ch!='\n')
    {
        while(ch!='\n')
        {
            ch=fgetc(fp1);
        }
    }
    Can be safely replaced simply with this:
    Code:
    while((ch=fgetc(fp1)) != '\n');
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char str[10];
    char str1[10],str2[10];
    ...
    str1[i++]=ch;
    ...
    str2[i++]=ch;
    ...
    str[i++]=ch;
    Your "str"-arrays can hold 10 characters but later in your code you never check if the words you read are longer thus it is very likely that your are out of bounds somewhere.

    I can't run your code now but after reading it I'm pretty sure your mac()-function doesn't work.

    Bye, Andreas

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    In the start of your main:
    Code:
    int main(void)
    {
        char ch;
        FILE *fp1, *fp2;
        if (fp1==NULL)
        {
            printf("Cant open file!");
            exit(0);
        }
    
    
    
    
        int flag=1;
        fp1=fopen("comm.txt","r");
        fp2=fopen("new.txt","w");
    
    
        ...

    I think that you've typed "if (fp1==NULL)" in the wrong spot and you don't check your new.txt has opened. Also, you have 2 versions of comm.txt open at the same time: Don't open comm.txt until after the mac() subroutine has been called.


    It should be something more like this


    Code:
    int main(void)
    {
        //char ch; Replaced by integer type as hk_mp5kpdw described...
        int in;
    
    
        FILE *fp1, *fp2;
        int flag=1;
    
    
        mac();
    
    
        fp1=fopen("comm.txt","r");
        fp2=fopen("new.txt","w");
    
    
        if ((fp1==NULL) || (fp2 == NULL))
        {
            printf("Cant open file!");
            exit(0);
        }
    
    
    
    
        ...
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Turn all of your global variables into local variables, fix your indentation, and apply other's advice,
    then I'll take a look at it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By tlman12 in forum C Programming
    Replies: 1
    Last Post: 10-29-2010, 03:57 PM
  2. Segmentation fault
    By Miffeltoffel in forum C Programming
    Replies: 8
    Last Post: 10-21-2010, 03:55 AM
  3. C Segmentation Fault Help
    By cow_tipper8282 in forum C Programming
    Replies: 5
    Last Post: 10-16-2010, 03:25 PM
  4. Segmentation Fault
    By dulipat in forum C Programming
    Replies: 3
    Last Post: 05-25-2010, 10:48 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21