Simple File I/O and Stream State question

The prompt is " Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isReadable otherwise. "


My code is:
1
2
3
4
5
6
ifstream topsecret;
isReadable = false;
if(topsecret.good())
{
     isReadable = true;
}


It is telling me that when they input an unreadable file my code sets isReadable to true, can anyone let me know where I went wrong?


I am also having an issue with this. "
Given an int variable x write some statements that attempt to open a file named "table20" and read a value into x ; if that turns out not to be possible your code should then read avalue from standard input into x . "

1
2
3
4
5
6
7
8
9
ifstream.open("table20")
if (table20.good())
{
	table20 >> x;
}
else if (table20.fail())
{
        cin >> x;
}


Thanks

Nevermind I fixed the issues
Last edited on
Topic archived. No new replies allowed.