bool AddHostToHosts(const char* src)
{
char sys_path[MAX_PATH] = { 0 };
if (!GetSystemDirectoryA(sys_path, MAX_PATH))
return false;
strcat(sys_path, "\\drivers\\etc\\hosts");
//检查内容是否存在
ifstream ifs(sys_path, ios::in);
string line;
if (!ifs.is_open())
return false;
while (!ifs.eof())
{
getline(ifs, line);
if (line.compare(src) == 0)
{
ifs.close();
return true;
}
}
ifs.close();
ofstream ofs; //写文件OFS
ofs.open(sys_path, ios::out | ios::app); //app追加
if (!ofs.is_open())
return false;
ofs << endl;
ofs << src << endl;
ofs.close();
return true;
}
bool DeleteHostFromHosts(const char* src)
{
char sys_path[MAX_PATH] = { 0 };
08-15
163
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-08
1792
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-25
4万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)