I try to read an int from a file using freopen(). File in.txt simply have a number:1, but what I get in output is -858993460. My code is shown below:
#include <cstdio>
#pragma warning(disable:4996)
using namespace std;
int main()
{
freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
printf("%d\n", t);
return 0;
}
Why does scanf() not read from in.txt correctly?
rule #1, if you have file io then check the return values
your code runs fine for me once I point it at a real file
also check the return from scanf