int test = 0;
ifstream inFile;
inFile.open("hat.txt");
inFile >> test;
cout << test;
I have the file in the debug folder where the .exe is running and the file of that file is hat.txt. All it has is one number. My question is why am I getting junk when I output ?
EDIT – Added the fail line and it does fail. Why is it failing?
int test;
ifstream inFile;
inFile.open("hat.txt");
if ( inFile.fail() )
{
cout << "It Failed" << endl;
}
inFile >> test;
cout << test;
Your code looks fine now that it has the failed check.
Why did it fail? probably the file doesn’t exist or has the wrong permissions.
I ran this code.
And it works fine. If hat.txt doesn’t exist or can’t be read then I get “It failed”. Without the failed check, I get random numbers outputed.
I think you’re problem is that it can’t read the file.