使用ofstream对象打开hosts文本的路径代码应该怎么写?

guangdongmutou 2010-07-19 11:16:42

想打开C:\Windows\System32\drivers\etc\中的hosts文本。
可是在电脑上打开它还要选择打开方式。

const char * hosts = "C:'\'Windows'\'System32'\'drivers'\'etc'\'hosts.txt";
ofstream outFile;
outFile.open (hosts);
//这样打不开
把不要.txt后缀也不行
const char * hosts = "C:'\'Windows'\'System32'\'drivers'\'etc'\'hosts";

请问题代码该怎么写?
...全文
73 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
guangdongmutou 2010-07-19
  • 打赏
  • 举报
回复
使用超级管理员身份运行成功了。。。

感谢!
太乙 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 guangdongmutou 的回复:]

引用 3 楼 hqin6 的回复:
ofstream ofs("C:\\Windows\\System32\\drivers\\etc\\hosts");---------不用单引号。。。

使用这个路径运行后,360提示有程序试图修改hosts文件,但最后
if (!outFile.is_open())
{
cout << "Could not open the file\n";
……
[/Quote]使用超级管理员身份运行你的程序。。。
guangdongmutou 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hqin6 的回复:]
ofstream ofs("C:\\Windows\\System32\\drivers\\etc\\hosts");---------不用单引号。。。
[/Quote]
使用这个路径运行后,360提示有程序试图修改hosts文件,但最后
if (!outFile.is_open())
{
cout << "Could not open the file\n";
cin.get();
return 0;
}
代码在打印了不能打开文件的提示。
太乙 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 guangdongmutou 的回复:]

C++中 \ 需要转义

不转义也试过了,不行
[/Quote]转义使用\而不是单引号

ofstream ofs("C:\\Windows\\System32\\drivers\\etc\\hosts");

\\就表示\
guangdongmutou 2010-07-19
  • 打赏
  • 举报
回复
C++中 \ 需要转义

不转义也试过了,不行
太乙 2010-07-19
  • 打赏
  • 举报
回复
ofstream ofs("C:\\Windows\\System32\\drivers\\etc\\hosts");---------不用单引号。。。
太乙 2010-07-19
  • 打赏
  • 举报
回复
ofstream ofs("C:\Windows\System32\drivers\etc\hosts");---------不用单引号。。。
liuwei2500 2010-07-19
  • 打赏
  • 举报
回复
"C:\\Windows\\System32\\drivers\\etc\\hosts.txt"

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧