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