The code below is trying to concatenate two text files and the output is written to another text file. As it's seen, I use yylex()
function to get the tokens from the input files. The yywrap()
function helps me to pass from the 1st text file to the 2nd text file and signals when EOF (End-Of-File) has been reached.
Why am I getting the error "Segmentation fault (core dumped)"
on my laptop, while on my colleague's it works properly?
I mention that I am using the terminal as root.
This is how I use the commands:
::-:lex tokdef.l
::-:cc lex.yy.c -o dan -ll
::-:./dan 1text 2text outtext
Segmentation fault (core dumped)
And this is my code:
%{
#include<string.h>
int second_file = 0;
char *nume2;
%}
%%
.;
\n;
%%
int main(int argc, char *argv[])
{
nume2=(char*)malloc(sizeof(char)*(strlen(argv[2])+1));
strcpy(nume2, argv[2]);
yyin = fopen(argv[1], "r");
yyout = fopen(argv[3], "w");
yylex();
fclose(yyin);
fclose(yyout);
}
int yywrap()
{
if(second_file == 0)
{
fclose(yyin);
yyin = fopen(nume2,"r");
second_file = 1;
return 0;
}
else
return 1;
}