What is a good way to test whether a file has required permissions?
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
add a comment |
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
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
add a comment |
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
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
c++ file permissions
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
Try the POSIX access() function, in unistd.h
add a comment |
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.
add a comment |
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.
add a comment |
You can use the chmod command.
It has 3 digits, corresponding to owner,group,other user
777 stands for max permissions.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Try the POSIX access() function, in unistd.h
add a comment |
Try the POSIX access() function, in unistd.h
add a comment |
Try the POSIX access() function, in unistd.h
Try the POSIX access() function, in unistd.h
answered Dec 11 '09 at 2:29
Paul BeckinghamPaul Beckingham
11.4k32663
11.4k32663
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 11 '09 at 2:32
Dirk EddelbuettelDirk Eddelbuettel
279k38515603
279k38515603
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 11 '09 at 2:36
Andrew KeithAndrew Keith
6,64411938
6,64411938
add a comment |
add a comment |
You can use the chmod command.
It has 3 digits, corresponding to owner,group,other user
777 stands for max permissions.
add a comment |
You can use the chmod command.
It has 3 digits, corresponding to owner,group,other user
777 stands for max permissions.
add a comment |
You can use the chmod command.
It has 3 digits, corresponding to owner,group,other user
777 stands for max permissions.
You can use the chmod command.
It has 3 digits, corresponding to owner,group,other user
777 stands for max permissions.
answered Nov 20 '18 at 11:22
Akash VermaAkash Verma
2029
2029
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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