在C ++中打开文件时如何设置文件权限? [英] How do I set file permissions when opening a file in C++?

查看:64
本文介绍了在C ++中打开文件时如何设置文件权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我想打开一个文件并设置其权限,但是失败了.下面是我的程序:

In C++, I want to open a file and set its permission, but I failed. Below is my program:

string filename="test.cnf";
ofstream ofile;
ofile.open(filename.c_str(),O_RDONLY);
ofile.close()

但是出现以下错误:

error: invalid conversion from 'int' to 'std::_Ios_Openmode'

error:   initializing argument 2 of 'void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]'

如何将文件的权限设置为例如644、700?

How to set the permission of the file to such as 644, 700?

推荐答案

fstream 没有 O_RDONLY 模式.您应使用以下一种:

There is not O_RDONLY mode for fstream. You should use one of:

  1. ios :: app
  2. ios :: binary
  3. ios :: ate
  4. ios :: in
  5. ios :: out
  6. ios :: trunc
  1. ios::app
  2. ios::binary
  3. ios::ate
  4. ios::in
  5. ios::out
  6. ios::trunc

或其组合(例如 ios :: app | ios :: binary ).对于您的情况,您应该使用 ios :: in (也许使用 ios :: binary ).有关更多详细信息,请参见.

Or their combinations (say ios::app | ios::binary). For your case you should use ios::in (maybe with ios::binary). Look at this for more details.

这篇关于在C ++中打开文件时如何设置文件权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆