What is a good way to test whether a file has required permissions?












2















I see that ifstream::open() returns void and does not offer any way to see if the file did not open due to permissions. What is a good api to test whether read permission or alternatively write permissions are available on a file for the current process in C++?










share|improve this question


















  • 1





    I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

    – Anders
    Dec 11 '09 at 2:30











  • Possible duplicate: stackoverflow.com/questions/1138508/…

    – Dan Hook
    Dec 11 '09 at 17:58











  • This is for Linux.

    – WilliamKF
    Dec 12 '09 at 0:30
















2















I see that ifstream::open() returns void and does not offer any way to see if the file did not open due to permissions. What is a good api to test whether read permission or alternatively write permissions are available on a file for the current process in C++?










share|improve this question


















  • 1





    I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

    – Anders
    Dec 11 '09 at 2:30











  • Possible duplicate: stackoverflow.com/questions/1138508/…

    – Dan Hook
    Dec 11 '09 at 17:58











  • This is for Linux.

    – WilliamKF
    Dec 12 '09 at 0:30














2












2








2








I see that ifstream::open() returns void and does not offer any way to see if the file did not open due to permissions. What is a good api to test whether read permission or alternatively write permissions are available on a file for the current process in C++?










share|improve this question














I see that ifstream::open() returns void and does not offer any way to see if the file did not open due to permissions. What is a good api to test whether read permission or alternatively write permissions are available on a file for the current process in C++?







c++ file permissions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 11 '09 at 2:23









WilliamKFWilliamKF

15k49145241




15k49145241








  • 1





    I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

    – Anders
    Dec 11 '09 at 2:30











  • Possible duplicate: stackoverflow.com/questions/1138508/…

    – Dan Hook
    Dec 11 '09 at 17:58











  • This is for Linux.

    – WilliamKF
    Dec 12 '09 at 0:30














  • 1





    I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

    – Anders
    Dec 11 '09 at 2:30











  • Possible duplicate: stackoverflow.com/questions/1138508/…

    – Dan Hook
    Dec 11 '09 at 17:58











  • This is for Linux.

    – WilliamKF
    Dec 12 '09 at 0:30








1




1





I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

– Anders
Dec 11 '09 at 2:30





I/O is OS specific, maybe if you could tell us your OS it would be possible to give a more specific answer.

– Anders
Dec 11 '09 at 2:30













Possible duplicate: stackoverflow.com/questions/1138508/…

– Dan Hook
Dec 11 '09 at 17:58





Possible duplicate: stackoverflow.com/questions/1138508/…

– Dan Hook
Dec 11 '09 at 17:58













This is for Linux.

– WilliamKF
Dec 12 '09 at 0:30





This is for Linux.

– WilliamKF
Dec 12 '09 at 0:30












4 Answers
4






active

oldest

votes


















2














Try the POSIX access() function, in unistd.h






share|improve this answer































    2














    You can also use stat which returns a bunch of information, including mode, uid and gid:



       struct stat {
    dev_t st_dev; /* ID of device containing file */
    ino_t st_ino; /* inode number */
    mode_t st_mode; /* protection */
    nlink_t st_nlink; /* number of hard links */
    uid_t st_uid; /* user ID of owner */
    gid_t st_gid; /* group ID of owner */
    dev_t st_rdev; /* device ID (if special file) */
    off_t st_size; /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t st_blocks; /* number of 512B blocks allocated */
    time_t st_atime; /* time of last access */
    time_t st_mtime; /* time of last modification */
    time_t st_ctime; /* time of last status change */
    };


    I am not aware of nice C++ wrappers for these lower-level functions.






    share|improve this answer































      1














      If your using windows , you can use GetFileAttributesEx to check the attributes of the file. If its read-only, you might need to call SetFileAttributes to unset the read-only flag.






      share|improve this answer































        0














        You can use the chmod command.
        It has 3 digits, corresponding to owner,group,other user
        777 stands for max permissions.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1885450%2fwhat-is-a-good-way-to-test-whether-a-file-has-required-permissions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Try the POSIX access() function, in unistd.h






          share|improve this answer




























            2














            Try the POSIX access() function, in unistd.h






            share|improve this answer


























              2












              2








              2







              Try the POSIX access() function, in unistd.h






              share|improve this answer













              Try the POSIX access() function, in unistd.h







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 11 '09 at 2:29









              Paul BeckinghamPaul Beckingham

              11.4k32663




              11.4k32663

























                  2














                  You can also use stat which returns a bunch of information, including mode, uid and gid:



                     struct stat {
                  dev_t st_dev; /* ID of device containing file */
                  ino_t st_ino; /* inode number */
                  mode_t st_mode; /* protection */
                  nlink_t st_nlink; /* number of hard links */
                  uid_t st_uid; /* user ID of owner */
                  gid_t st_gid; /* group ID of owner */
                  dev_t st_rdev; /* device ID (if special file) */
                  off_t st_size; /* total size, in bytes */
                  blksize_t st_blksize; /* blocksize for file system I/O */
                  blkcnt_t st_blocks; /* number of 512B blocks allocated */
                  time_t st_atime; /* time of last access */
                  time_t st_mtime; /* time of last modification */
                  time_t st_ctime; /* time of last status change */
                  };


                  I am not aware of nice C++ wrappers for these lower-level functions.






                  share|improve this answer




























                    2














                    You can also use stat which returns a bunch of information, including mode, uid and gid:



                       struct stat {
                    dev_t st_dev; /* ID of device containing file */
                    ino_t st_ino; /* inode number */
                    mode_t st_mode; /* protection */
                    nlink_t st_nlink; /* number of hard links */
                    uid_t st_uid; /* user ID of owner */
                    gid_t st_gid; /* group ID of owner */
                    dev_t st_rdev; /* device ID (if special file) */
                    off_t st_size; /* total size, in bytes */
                    blksize_t st_blksize; /* blocksize for file system I/O */
                    blkcnt_t st_blocks; /* number of 512B blocks allocated */
                    time_t st_atime; /* time of last access */
                    time_t st_mtime; /* time of last modification */
                    time_t st_ctime; /* time of last status change */
                    };


                    I am not aware of nice C++ wrappers for these lower-level functions.






                    share|improve this answer


























                      2












                      2








                      2







                      You can also use stat which returns a bunch of information, including mode, uid and gid:



                         struct stat {
                      dev_t st_dev; /* ID of device containing file */
                      ino_t st_ino; /* inode number */
                      mode_t st_mode; /* protection */
                      nlink_t st_nlink; /* number of hard links */
                      uid_t st_uid; /* user ID of owner */
                      gid_t st_gid; /* group ID of owner */
                      dev_t st_rdev; /* device ID (if special file) */
                      off_t st_size; /* total size, in bytes */
                      blksize_t st_blksize; /* blocksize for file system I/O */
                      blkcnt_t st_blocks; /* number of 512B blocks allocated */
                      time_t st_atime; /* time of last access */
                      time_t st_mtime; /* time of last modification */
                      time_t st_ctime; /* time of last status change */
                      };


                      I am not aware of nice C++ wrappers for these lower-level functions.






                      share|improve this answer













                      You can also use stat which returns a bunch of information, including mode, uid and gid:



                         struct stat {
                      dev_t st_dev; /* ID of device containing file */
                      ino_t st_ino; /* inode number */
                      mode_t st_mode; /* protection */
                      nlink_t st_nlink; /* number of hard links */
                      uid_t st_uid; /* user ID of owner */
                      gid_t st_gid; /* group ID of owner */
                      dev_t st_rdev; /* device ID (if special file) */
                      off_t st_size; /* total size, in bytes */
                      blksize_t st_blksize; /* blocksize for file system I/O */
                      blkcnt_t st_blocks; /* number of 512B blocks allocated */
                      time_t st_atime; /* time of last access */
                      time_t st_mtime; /* time of last modification */
                      time_t st_ctime; /* time of last status change */
                      };


                      I am not aware of nice C++ wrappers for these lower-level functions.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 11 '09 at 2:32









                      Dirk EddelbuettelDirk Eddelbuettel

                      279k38515603




                      279k38515603























                          1














                          If your using windows , you can use GetFileAttributesEx to check the attributes of the file. If its read-only, you might need to call SetFileAttributes to unset the read-only flag.






                          share|improve this answer




























                            1














                            If your using windows , you can use GetFileAttributesEx to check the attributes of the file. If its read-only, you might need to call SetFileAttributes to unset the read-only flag.






                            share|improve this answer


























                              1












                              1








                              1







                              If your using windows , you can use GetFileAttributesEx to check the attributes of the file. If its read-only, you might need to call SetFileAttributes to unset the read-only flag.






                              share|improve this answer













                              If your using windows , you can use GetFileAttributesEx to check the attributes of the file. If its read-only, you might need to call SetFileAttributes to unset the read-only flag.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 11 '09 at 2:36









                              Andrew KeithAndrew Keith

                              6,64411938




                              6,64411938























                                  0














                                  You can use the chmod command.
                                  It has 3 digits, corresponding to owner,group,other user
                                  777 stands for max permissions.






                                  share|improve this answer




























                                    0














                                    You can use the chmod command.
                                    It has 3 digits, corresponding to owner,group,other user
                                    777 stands for max permissions.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      You can use the chmod command.
                                      It has 3 digits, corresponding to owner,group,other user
                                      777 stands for max permissions.






                                      share|improve this answer













                                      You can use the chmod command.
                                      It has 3 digits, corresponding to owner,group,other user
                                      777 stands for max permissions.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 20 '18 at 11:22









                                      Akash VermaAkash Verma

                                      2029




                                      2029






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1885450%2fwhat-is-a-good-way-to-test-whether-a-file-has-required-permissions%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                          ComboBox Display Member on multiple fields

                                          Is it possible to collect Nectar points via Trainline?