0

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;
}
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Dani Grosu
  • 544
  • 1
  • 4
  • 22

1 Answers1

2

I figure it out what was the problem. So it doesn't matter how do I use the terminal (root or regular). The problem was linked to the input files (1text and 2text). Firstly, I created the 1text and to create the 2text, file I just copied and paste the 1text file and rename it as 2text. So, my next question: What happens when I copy and paste a file in the same folder?

Dani Grosu
  • 544
  • 1
  • 4
  • 22
  • 1
    I think have the answer for this too: when you copy and paste a file, both have the same inode on the hard disk, so this could be the root of the problem. – Dani Grosu Mar 06 '16 at 15:10