ofstream open bit

S

sam

Hi,

The following C++ line can't pass the c++ compiler:

ofstream omfile;
omfile.open(f, ios::eek:ut | ios::trunc, 0700);

In C++, is there anyway I can create a file with the permission bit set
to executable by root?

Sam
 
S

Siemel Naran

sam said:
ofstream omfile;
omfile.open(f, ios::eek:ut | ios::trunc, 0700);

In C++, is there anyway I can create a file with the permission bit set
to executable by root?

There's no way in standard C++ because executable by root is a UNIX concept.
 
S

Stephen Howe

In C++, is there anyway I can create a file with the permission bit set to
executable by root?

No, not at all. But I would suggest searching your compilers documentation.
They might have a non-standard constructor for ofstream such that you can do
it.

But it is not part of standard C++. It would a be an extension.

Stephen Howe
 
L

Lionel B

sam said:
Then, I will to use C API to do so.

Since "executable by root" is OS specific, perhaps it would be cleaner to separate out the permission setting part; i.e.
open/create your file as per standard C++ and then use the appropriate system() call (chmod or whatever) to set
permissions. That way it is clear to someone reading your code that there is something OS specific going on. Is this
what you meant by "use C API"?

[As an aside, I'm not sure how you'd ascertain the success or otherwise of your system() call... I think the standard
library system() simply returns the status of the *shell* - if such exists - invoked to execute your command].
 
S

sam

Lionel said:
Since "executable by root" is OS specific, perhaps it would be cleaner to separate out the permission setting part; i.e.
open/create your file as per standard C++ and then use the appropriate system() call (chmod or whatever) to set
permissions. That way it is clear to someone reading your code that there is something OS specific going on. Is this
what you meant by "use C API"?
Yeah, this is what I intended to do now. I used popen C API execute the
change of the file permission bit. But I don;t like the idea of use
popen() or exec(). Because I think this should be able do when creating
the file.

However, I must live with it for the time being. Perhas C++ extension
such as ACE_OS might be have a way to do that.

Thanks
Sam

[As an aside, I'm not sure how you'd ascertain the success or otherwise of your system() call... I think the standard
library system() simply returns the status of the *shell* - if such exists - invoked to execute your command].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top