Optimization of C code- prevent multiple file openings
<pre><code>yyin = fopen("input_file.txt","r");
while ((nread = fread(buf, 1, sizeof buf, yyin)) &gt; 0){
   fwrite(buf, 1, nread, stdout);
 }
fclose(yyin);
yyin = fopen("input_file.txt","r");
yyparse();
fclose(yyin);
</code></pre>

<p>As you see, I open multiple times the same file. First I want to print the output in my cmd/terminal; then I want to start the parser. The problem is obvious: Each opening process costs resources, but the combination of fread and fwrite seems to "clean" my input file so that yyparse does not get any further input and basically stays empty.</p>

<p>Do you have any idea of how to make this more performant or smooth with out 2 file openings?</p>