I would like to know how to check if I have write permissions to a folder.
I’m writing a C++ project and I should print some data to a result.txt file, but I need to know if I have permissions or not.
Is the check different between Linux and Windows? Because my project should run on Linux and currently I’m working in Visual Studio.
The portable way to check permissions is to try to open the file and check if that succeeded. If not, and
errno(from the header<cerrno>is set to the valueEACCES[yes, with one S], then you did not have sufficient permissions. This should work on both Unix/Linux and Windows. Example for stdio:Iostreams will work a bit differently. AFAIK, they do not guarantee to set
errnoon both platforms, or report more specific errors than just “failure”.As Jerry Coffin wrote, don’t rely on separate access test functions since your program will be prone to race conditions and security holes.