简体   繁体   中英

Using Output file as Input file in same program in cpp

I was wondering is it possible to write test case file and use that test cases as input file and produce another output file in the same program in c++.

So far, I have tried this:

freopen("input.txt","w",stdout);
for(int i=2;i<=20000;i++)
    cout<<i<< " ";
fclose(stdout);
cout<<"Finished writing test cases"<<endl;
freopen("input.txt","r", stdin);
freopen("output.txt","w",stdout);
....
/*rest of the code including where I write stuff to output.txt*/

I opened my input and output files. Input is okay, but output.txt does not show any output.

Any help would be appreciated, thanks.

Remove this line :

fclose(stdout);

Put this line at the end of the program, or after the part when you are sure you won't write to a file instead of stdout .

I think the code will work as you expect it to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM