Thread: segmentation fault

  1. #1
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24

    segmentation fault

    i don't realise why this program gives segmentation fault when executed... here's the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char* argv[]){
      FILE *in, *out, *includefile;
      match = ( char *) malloc( BUFSIZ );
      line = ( char * ) malloc ( BUFSIZ );
      includefilename = ( char * ) malloc ( BUFSIZ );
      char *p;
      int c,i,d;
      i=d=0;
      if (argc == 3){  
        in = fopen ( argv[1], "r" );
        if ( in == NULL ) {
          perror ( NULL );
          return EXIT_FAILURE ;
        }
        
        out = fopen ( argv[2], "w" );
        if ( out == NULL ) {
          perror ( NULL );
          fclose ( in );
          return EXIT_FAILURE;
        }
           while ( fgets ( match, sizeof ( match ), in ) != NULL ) {
          if ( ( p = strstr ( match, "#incluir" ) ) != NULL ) {
    	while( match[10+d] != '\"'){
    	  includefilename[i] = c;
    	  i++;}
    	i=d=0;
    	includefile = fopen ( includefilename, "r" );
    	while ( fgets (line, sizeof line , includefile ) != NULL ){
    	  fprintf ( out, "%s", line );
    	  free (line);
    	  free (match);
    	  fclose (includefile);}	
          }
          else {
    	fprintf ( out, "%s", match );
    	free ( match);
    	
          }
        }
      }
      else {
        return EXIT_FAILURE;}
      fclose (in);
      fclose (out);
      return EXIT_SUCCESS;
    }
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It doesn't even compile.

    The variable c is used before it is given any specific value.

    Have you tried debugging this yourself yet? Put some printf()'s in there to see whereabouts the problems occur.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    well i tried and i continue trying... but i will use that printf suggestion.. hope i can find where the error occurs it is driving me nuts!!!
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  4. #4
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    thanks a lot hammer! part of my preprocessor is now done!
    when things go right c programming is fuuun!!
    moving to the next step, replacing the #define with something else... thanks to prelude to, he was the one who lit the way!!!
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes he was.....



    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM