To read the input from a text file, I wrote the following code:
int main(){
int x;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &x);
printf("%d\n", x);
system("pause");
return 0;
}
It works pretty well.
However, in visual studio, the compiler gives me an error due to freopen and suggests that I use freopen_s instead. I am trying to understand how the freopen_s function works, but I cannot. My code is:
int main(){
int x;
#ifndef ONLINE_JUDGE
FILE *stream;
freopen_s(&stream, "input.txt", "r", stdin);
#endif
scanf("%d", &x);
printf("%d\n", x);
system("pause");
return 0;
}
It does not output anything, and even a “pause” does not work. CMD disappears immediately after the program ends without printing. I do not know why the "stream" is used in freopen_s. Thanks for answering my question.
source
share