use
freopen("input.txt", "r", stdin);
to take input from input.txt
freopen("output.txt", "r", stdout);
to save output to output.txt
You can use
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin); - to take input use “r”=read
freopen(“output.txt”, “w”, stdout); - to output use “w”=write
#endif