0

I created a program to append line numbers to the text file passed as an argument to the program. yyin is working fine, but yyout is not working i.e, contents are being read from the specified file, but nothing is being written to the output file instead the output is display to the console.

%option noyywrap
%{
#include<stdio.h>
int linenumber=0;
%}
%%
^(.*)\n printf("%4d\t%s",++linenumber,yytext);
%%
int main()
{
yyin=fopen("test.c","r");
yyout=fopen("result.txt","w");
yylex();
}
pegasus
  • 21
  • 6
Rakshith
  • 11
  • 2
  • 1
    Please provide your program and output as text in the answer, rather than screenshots with external links. – Pyves Jun 02 '17 at 17:55

1 Answers1

1

If you want to output to yyout, why do you use printf?

(F)lex does not magically change the operation of the standard library. printf continues to print to stdout, as per its documentation. If you wanted to output to yyout, you would use

fprintf(yyout, ...);

Flex only uses yyout in its own ECHO action, which is the default action for any unmatched text, and which you can use explicitly in a rule. For example, you could have written that action as:

.*\n   { fprintf(yyout, "%4d\t", ++linenumber); ECHO; }
rici
  • 234,347
  • 28
  • 237
  • 341