In unix if you create and open a file by using 9 digits numbers in the open function it seems to work but in C++ you only don't have a parameter to do that rather you have ios_base. Just wondering if there are owner,group and world settings that can be set on a file when it is created in C++?

Dani AI

Generated

— useful follow-up to ’s pointer. Below are practical, reliable ways to control Unix-style owner/group/permission bits from C++, plus brief notes on pitfalls to avoid.

Create with the right mode (POSIX)

  • Use the OS open(2) call with O_CREAT and a mode_t (e.g. 0644) to create a file with the desired permission bits. Be aware the process umask will mask bits out, so either temporarily adjust umask or apply fchmod after creation if you need exact bits. See open(2) and umask(2) for details: open(2), umask(2).

Change permissions or ownership after creation

  • Use fchmod/chmod to set permission bits, and chown to change owner/group (changing owner usually requires root privileges). If you prefer sticking with C++ streams for I/O, create the file with std::ofstream then call std::filesystem::permissions (C++17+) or chmod/fchmod to adjust bits. See fchmod(2), chown(2), and std::filesystem::permissions: fchmod(2), chown(2), std::filesystem::permissions.

Gotchas and recommendations

  • umask is global to the process and can affect other threads; changing it temporarily has race/thread-safety concerns.
  • Doing create-then-change can introduce a tiny race window; if that is unacceptable, prefer creating via open(2) with the correct mode and manage umask carefully.
  • Owner/group settings are OS-level and not portable; for cross-platform code you must branch (Windows uses its own APIs). For ownership changes you will usually need elevated privileges.

These options cover the typical needs: create with desired mode (POSIX), or create then adjust using filesystem/OS calls.

Recommended Answers

All 2 Replies

File permissions are platform specific, so you only get access to them using platform specific APIs.

that's what i thought. thanks for help. Sorry for all the questions teacher doesn't really explain anything about file structures and the book we are using is super old. .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.