Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 150903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:21:52+00:00 2026-05-11T09:21:52+00:00

I am using C++ ofstream to write out a file. I want to set

  • 0

I am using C++ ofstream to write out a file. I want to set the permissions to be only accessible by the user: 700. In unix; I suppose I can just issue a system('chmod 700 file.txt'); but I need this code to work on Windows as well. I can use some Windows api; but what is the best c++ cross platform way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:21:53+00:00Added an answer on May 11, 2026 at 9:21 am

    Ironically, I have just run into this very same need earlier today.

    In my case, the answer came down to what level of permission granularity I need on Windows, versus Linux. In my case, I only care about User, Group, and Other permission on Linux. On Windows, the basic Read/Write All permission leftover from DOS is good enough for me, i.e. I don’t need to deal with ACL on Windows.

    Generally speaking, Windows has two privilege models: the basic DOS model and the newer access control model. Under the DOS model there is one type of privilege: write privilege. All files can be read, so there is no way to turn off read permission (because it doesn’t exist). There is also no concept of execute permission. If a file can be read (answer is yes) and it is binary, then it can be executed; otherwise it can’t.

    The basic DOS model is sufficient for most Windows environments, i.e. environments where the system is used by a single user in a physical location that can be considered relatively secure. The access control model is more complex by several orders of magnitude.

    The access control model uses access control lists (ACL) to grant privileges. Privileges can only be granted by a process with the necessary privileges. This model not only allows the control of User, Group, and Other with Read, Write, and Execute permission, but it also allows control of files over the network and between Windows domains. (You can also get this level of insanity on Unix systems with PAM.)

    Note: The Access Control model is only available on NTFS partitions, if you are using FAT partitions you are SOL.

    Using ACL is a big pain in the ass. It is not a trivial undertaking and it will require you to learn not just ACL but also all about Security Descriptors, Access Tokens, and a whole lot of other advanced Windows security concepts.

    Fortunately for me, for my current needs, I don’t need the true security that the access control model provides. I can get by with basically pretending to set permissions on Windows, as long as I really set permissions on Linux.

    Windows supports what they call an ‘ISO C++ conformant’ version of chmod(2). This API is called _chmod, and it is similar to chmod(2), but more limited and not type or name compatible (of course). Windows also has a deprecated chmod, so you can’t simply add chmod to Windows and use the straight chmod(2) on Linux.

    I wrote the following:

    #include <sys/stat.h> #include <sys/types.h>  #ifdef _WIN32 #   include <io.h>  typedef int mode_t;  /// @Note If STRICT_UGO_PERMISSIONS is not defined, then setting Read for any ///       of User, Group, or Other will set Read for User and setting Write ///       will set Write for User.  Otherwise, Read and Write for Group and ///       Other are ignored. /// /// @Note For the POSIX modes that do not have a Windows equivalent, the modes ///       defined here use the POSIX values left shifted 16 bits.  static const mode_t S_ISUID      = 0x08000000;           ///< does nothing static const mode_t S_ISGID      = 0x04000000;           ///< does nothing static const mode_t S_ISVTX      = 0x02000000;           ///< does nothing static const mode_t S_IRUSR      = mode_t(_S_IREAD);     ///< read by user static const mode_t S_IWUSR      = mode_t(_S_IWRITE);    ///< write by user static const mode_t S_IXUSR      = 0x00400000;           ///< does nothing #   ifndef STRICT_UGO_PERMISSIONS static const mode_t S_IRGRP      = mode_t(_S_IREAD);     ///< read by *USER* static const mode_t S_IWGRP      = mode_t(_S_IWRITE);    ///< write by *USER* static const mode_t S_IXGRP      = 0x00080000;           ///< does nothing static const mode_t S_IROTH      = mode_t(_S_IREAD);     ///< read by *USER* static const mode_t S_IWOTH      = mode_t(_S_IWRITE);    ///< write by *USER* static const mode_t S_IXOTH      = 0x00010000;           ///< does nothing #   else static const mode_t S_IRGRP      = 0x00200000;           ///< does nothing static const mode_t S_IWGRP      = 0x00100000;           ///< does nothing static const mode_t S_IXGRP      = 0x00080000;           ///< does nothing static const mode_t S_IROTH      = 0x00040000;           ///< does nothing static const mode_t S_IWOTH      = 0x00020000;           ///< does nothing static const mode_t S_IXOTH      = 0x00010000;           ///< does nothing #   endif static const mode_t MS_MODE_MASK = 0x0000ffff;           ///< low word  static inline int my_chmod(const char * path, mode_t mode) {     int result = _chmod(path, (mode & MS_MODE_MASK));      if (result != 0)     {         result = errno;     }      return (result); } #else static inline int my_chmod(const char * path, mode_t mode) {     int result = chmod(path, mode);      if (result != 0)     {         result = errno;     }      return (result); } #endif 

    It’s important to remember that my solution only provides DOS type security. This is also known as no security, but it is the amount of security that most apps give you on Windows.

    Also, under my solution, if you don’t define STRICT_UGO_PERMISSIONS, when you give a permission to group or other (or remove it for that matter), you are really changing the owner. If you didn’t want to do that, but you still didn’t need full Windows ACL permissions, just define STRICT_UGO_PERMISSIONS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing a file using ofstream, how do I know when the file's size
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I want serialize object to binary file using operator <<, but when I serialize,
I want to write an Item to a binary file, close it, and open
I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
For my OS class I'm supposed to implement Linux's cat using only system calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.