2
yyin = fopen("input_file.txt","r");
while ((nread = fread(buf, 1, sizeof buf, yyin)) > 0){
   fwrite(buf, 1, nread, stdout);
 }
fclose(yyin);
yyin = fopen("input_file.txt","r");
yyparse();
fclose(yyin);

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.

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

1 Answer 1

8

You can use rewind() to return to the beginning of the file again. See this documentation for more details.

1
  • Perfect! That's exactly what I looked for. Thank you very much. Apr 1, 2016 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.