!file trickiness
-
I have the following file code:
#include
#include
using namespace std;int main() {
//add code below this line string path = "student/text/readpractice.txt"; try { ifstream file; file.open(path); //content of file goes into memory buffer if (!file) { throw runtime\_error("File failed to open."); } cout << file.rdbuf(); //read the buffered content file.close(); } catch (exception& e) { cerr << e.what() << endl; } //add code above this line return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.
-
I have the following file code:
#include
#include
using namespace std;int main() {
//add code below this line string path = "student/text/readpractice.txt"; try { ifstream file; file.open(path); //content of file goes into memory buffer if (!file) { throw runtime\_error("File failed to open."); } cout << file.rdbuf(); //read the buffered content file.close(); } catch (exception& e) { cerr << e.what() << endl; } //add code above this line return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.
There is an operator! in ifstream [std::basic_ios<CharT,Traits>::operator! - cppreference.com](https://en.cppreference.com/w/cpp/io/basic\_ios/operator!) [https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open\](https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open)
CI/CD = Continuous Impediment/Continuous Despair
-
There is an operator! in ifstream [std::basic_ios<CharT,Traits>::operator! - cppreference.com](https://en.cppreference.com/w/cpp/io/basic\_ios/operator!) [https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open\](https://stackoverflow.com/questions/59919741/what-is-the-difference-between-file-and-file-is-open)
CI/CD = Continuous Impediment/Continuous Despair
Interesting. I tried doing a "Step Into" on that line in the debugger, but it won't go to it. If I do a "Go to Definition", it goes to this in xiosbase:
\_NODISCARD bool \_\_CLR\_OR\_THIS\_CALL operator!() const noexcept /\* strengthened \*/ { return fail(); }
However, I put a breakpoint there, and it doesn't break.
-
I have the following file code:
#include
#include
using namespace std;int main() {
//add code below this line string path = "student/text/readpractice.txt"; try { ifstream file; file.open(path); //content of file goes into memory buffer if (!file) { throw runtime\_error("File failed to open."); } cout << file.rdbuf(); //read the buffered content file.close(); } catch (exception& e) { cerr << e.what() << endl; } //add code above this line return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class? Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded. Any ideas? Thanks.
mike7411 wrote:
Can someone help me understand how this line works:
the statement:
if (!file)
means if the variable
file
equalsnull
then ... But you have already instantiatedfile
with the line:ifstream file;
So it is very unlikely that
file
will benull
. Theopen
method sets thefailbit
status if the open fails, and that is what you need to test. If you use STL classes then you must follow the STL rules, not the old C-style ones. See std::basic_ifstream<CharT,Traits>::open - cppreference.com[^] for full details.