Jump to content

can't find mistake....

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Master_C

Master_C

    Newbie

  • Members
  • Pip
  • 4 posts
I wrote this function:

void includes_extractor(FILE *c1_fp, char *c1_file_name ,int c1_file_str_len )

{

	int i=0;

	FILE *c2_fp , *header_fp;

	char ch, *c2_file_name,header_name[80]; /* we can assume line length 80 chars MAX*/

	char inc_name[]="include"; 

	char inc_chk[INCLUDE_LEN+1]; /*INCLUDE_LEN is defined | +1 for null*/


	/* making the c2 file name */


	c2_file_name=(char *) malloc ((c1_file_str_len)*sizeof(char));

	if (c2_file_name == NULL)

	{

	 printf("Out of memory !\n");

	 exit(0);

	} 


	strcpy(c2_file_name , c1_file_name); 

	c2_file_name[c1_file_str_len-1] = '\0'; 

	c2_file_name[c1_file_str_len-2] = '2';


/*Open source & destination files + ERR check */


	if( !(c1_fp = fopen (c1_file_name,"r") ) )

	{

	 fprintf(stderr,"\ncannot open *.c1 file !\n");

	 exit(0);

	}

	

	if( !(c2_fp = fopen (c2_file_name,"w+") ) )

	{

	 fprintf(stderr,"\ncannot open *.c2 file !\n");

	 exit(0);

	}


/*next code lines are copy char by char from c1 to c2,

  but if meet header file, copy its content */


	ch=fgetc(c1_fp);

	while (!feof(c1_fp))

	{

		i=0;	/*zero i */	

		if (ch == '#') /*potential #include case*/

		{

	         fgets(inc_chk, INCLUDE_LEN+1, c1_fp); /*8 places for "include" + null*/

		 if(strcmp(inc_chk,inc_name)==0) /*case #include*/

		 {

		  ch=fgetc(c1_fp);

		  while(ch==' ') /* stop when head with a '<' or '"' */

		  {

		   ch=fgetc(c1_fp);

		  } /*while(2)*/

		  

		  ch=fgetc(c1_fp); /*start read header file name*/


		  while((ch!='"') || (ch!='>')) /*until we get the end of header name*/

		  {

		   header_name[i] = ch;

		   i++;

		   ch=fgetc(c1_fp);

		  }/*while(3)*/

		  header_name[i]='\0';  /*close the header_name array*/

		  


		  if( !(header_fp = fopen (header_name,"r") ) ) /*open *.h for read + ERR chk*/

		  {

	           fprintf(stderr,"cannot open header file !\n");

	 	   exit(0);

	          }

		  while (!feof(header_fp)) /*copy header file content to *.c2 file*/

		  {

		   ch=fgetc(header_fp);

		   fputc(ch,c2_fp);

		  }/*while(4)*/

		  fclose(header_fp);

		 }

                }/*frst if*/

		else

		{

		 fputc(ch,c2_fp);

		}

	 ch=fgetc(c1_fp); 

	}/*while(1)*/ 


fclose(c1_fp);

fclose(c2_fp);

free (c2_file_name); 	

}

This function get a anyFileName.c1 file and create a new anyFileName.c2 that is a copy of anyFileName.c1 BUT in anyFileName.c2 all the #include files content written instead of the include calls.

The debugger gives me a 'segmentation fault' for some unknown reason.

I need just to know were is the bug here, and I can't find...

Please help, I'm desperate with this...

Thanks:crying:

#2
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Did you write this function or copy it from someone else's code?
It doesn't make sense that the writer of this code wouldn't know how to fix a segmentation fault.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#3
Master_C

Master_C

    Newbie

  • Members
  • Pip
  • 4 posts
I can swear it's mine !!

I wrote its main also, and believe or not, 2 days bleed on it...

This is only a part of a whole program that immitates the pre-processor 3 steps...
This is the second step of the PP.

If you can ALSO get me to the third step of the PP source code (deal with enum+MACRO) it will be great!
:crying:

#4
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Alright alright, umm. have you tried to step through it with a debugger? That can be an easy way to determine which line the error is generated on

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#5
Master_C

Master_C

    Newbie

  • Members
  • Pip
  • 4 posts
Tried already...

Tried everything but I'm getting crazy of it... tried also change my code (it was Dynamic allocation for the header_name. I state again to 80 = MAX line length ), but nothing help...

#6
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
what line was the error generated on?

Macros aren't exactly the easiest thing to deal with. When generating ctag files with exuberant the original headers had to be edited because parsing the macros is a beezy.
You are taking on quite a difficult task.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#7
Master_C

Master_C

    Newbie

  • Members
  • Pip
  • 4 posts
The error was refer to the while((ch!='"') || (ch!='>')). I changed it to while((ch!='"') && (ch!='>')).

Done. BUT.. getting an annoying ASCII char after every include extracted. how can I avoid them appearance ?

Edited by Master_C, 04 February 2011 - 04:32 PM.


#8
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
It's gotta be in the write to c2 file instructions. review that carefully.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users