【发布时间】:2011-11-05 03:13:04
【问题描述】:
我正在编写一个管理主机文件条目的应用程序。所以我用 C++ 写了一小段代码,试图访问和读取 HOSTS 文件:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
int main(void)
{
string line;
fstream f ("C:\Windows\System32\drivers\etc\hosts");
if ( f.is_open() )
{
while ( f.good() )
{
getline(f,line);
cout << line << endl;
}
f.close();
} else
cout << "Error" << endl;
system("pause");
return 0;
}
在提出这个问题之前,我已经阅读了这个问题:edit the etc\hosts file
所以,是的,我尝试以管理员身份运行该程序,但它仍然无法正常工作。我的程序如何读取/编辑以管理员身份运行的主机?
【问题讨论】:
-
f.is_open()返回false,表示fstream f ("C:\\Windows\\System32\\drivers\\etc\\hosts");无法打开hosts文件。