-
Notifications
You must be signed in to change notification settings - Fork 94
fix #381 #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #381 #382
Conversation
simplecpp.cpp
Outdated
|
||
// This is a workaround to properly check if a file exists, | ||
// since std::ifstream::good() incorrectly returns true for directories | ||
std::fstream fs(simplePath, std::ios::app); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is an interesting handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution still has flaws; when the file does not exist, an empty file will be created. Therefore, I replaced it with
std::fstream fs(simplePath, std::ios::in|std::ios::out);
Here is a simple test result (linux, mac, and windows):
https://github.com/foryoung365/CppFstreamTest/actions/runs/11549258974
Here are some references:
https://en.cppreference.com/w/cpp/io/basic_filebuf/open
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI I used this in #435 to improve the checks in the CLI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That check is also flawed because it will fail if the files are read-only.
Thanks. Please give me a few days since I might be out sick. |
how do we test this? you can easily check if a file exists or not in a unit test but if you need to test various edge cases I guess a pytest working in a tempdir is better. |
The issue referenced here was fixed in #443. So I think this can be closed. And #339 contains improved code for checking for the existence of a file/directory. I also filed https://trac.cppcheck.net/ticket/14133 about detecting such code with Cppcheck. |
File existence check using fstream with ios::app is implemented as a workaround because: