使用c ++或直接更改主机文件

时间:2017-07-26 13:09:13

标签: c++ host hosts

我想阻止      浏览器上的网站     通过更改主机文件。但是这个代码给了我"没有权限 。甚至可以直接更改主机文件更改权限或以管理员身份打开它。

#include<fstream>
#include<iostream>
#include<conio.h>

using namespace std;
int main()
 {
    // clrscr();
     char site[20],ch;
     ifstream in;
     ofstream out;
     int i;

     cout<<"1.block site \n2.unblock all site\n\nEnter the value:";
     cin>>i;

     if(i==1)
     {

     cout<<"\nEnter the Name of the Site to Block :";
     cin>>site;

     out.open("C:\Windows\System32\drivers\etc\hosts",ios::app);
     if(!out)
     cout<<"File Not Found or Permission Denied";
     else
     {
     out<<"\n127.0.0.1"<<"\t"<<site;
     out<<"\n127.0.0.1"<<"\twww."<<site;
     out<<"\n127.0.0.1"<<"\thttp://"<<site;
     out<<"\n127.0.0.1"<<"\thttp://www."<<site;
     out<<"\n127.0.0.1"<<"\thttps://"<<site;
     out<<"\n127.0.0.1"<<"\thttps://www."<<site;
    cout<<"\n"<<site;
    cout<<" is blocked";
    }
     out.close();
    }
    else if(i==2)
    {

     out.open("C:\Windows\system32\drivers\etc\hosts");
     if(!out)
     {
     cout<<"File Not Found(not for linux)";
     }
     else
     {
     out<<"\n127.0.0.1"<<"\t"<<"localhost";
     cout<<"\nAll site are unblocked";

     }
     out.close();}
     else
     {
     cout<<"\nEnter proper value";
     }
     getch();
 }

0 个答案:

没有答案