While posting that article, We thought that this tutorial is incomplete as we had not provided you the program to unblock the web site. So, here we are. We coded this program that can unblock the web sites on your computer that have been blocked through editing the hosts file.
Windows 7 requires extra treatment. Administrative privileges are required. Either run Codeblock as Administrator then run the program or compile it, execute it first time (it won’t work this time), go to the location of the generated EXE file, run the EXE as the administrator. Better go with first option. This program was coded to work with CodeBlock. Download CodeBlock mingw-setup from here.
Here is the program.
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int check(char[],char[]);
int main()
{
char ch,site[50],test[500];
int pointer=0,next[100],i=2,last[100];
int k,j,len,flag;
next[1]=1;
cout<<"Enter the name of the site you wish to UNBLOCK ";
cin>>site;
len=strlen(site);
ifstream in,sec;
ofstream out;
in.open("C:/Windows/System32/drivers/etc/hosts",ios::in);
if(!in)
cout<<"File not found";
else
{
while(in.get(ch))
{
if(ch==10)
{
pointer=pointer+2;
next[i]=pointer;
i++;
}
else
pointer=pointer+1;
}
next[i]=pointer;
i++;
}
in.close();
for(j=1;j<i-1;j++)
{
if(j==(i-2))
last[j]=next[j+1]-1;
else
last[j]=next[j+1]-3;
}
sec.open("C:/Windows/System32/drivers/etc/hosts",ios::in);
if(!sec)
cout<<"File Not Found";
else
{
for(j=1;j<i-1;j++)
{
k=0;
pointer=next[j];
sec.seekg(next[j],ios::beg);
while(pointer<=last[j])
{
sec.get(ch);
test[k]=ch;
k++;
pointer++;
}
test[k]='\0';
flag=check(test,site);
if(flag==1)
{
flag=j;
break;
}
}
sec.close();
}
if(flag==0)
cout<<"Entry in the Hosts File Not Found";
else
{
out.open("C:/Windows/System32/drivers/etc/hosts",ios::in);
if(!out)
cout<<"File not found";
else
{
ch=' ';
pointer=next[flag];
out.seekp(next[flag],ios::beg);
while(pointer<=last[flag])
{
out.put(ch);
pointer++;
}
out.close();
}
if(pointer==(last[flag]+1))
cout<<"Entery Has Been Edited";
else
cout<<"Permission has been denied";
}
return 0;
}
int check(char a[500],char b[100])
{
int i,j,k=0;
for(i=0;a[i]!='\0';i++)
{
if(a[i]==b[0])
{
k=1;
for(j=1;b[j]!='\0';j++)
{
if(a[j+i]!=b[j])
{
k=0;
break;
}
}
if(k==1)
return k;
}
}
return 0;
}
Also Read : Block any Website on Computer using C Code
Hope you like the post, comment bellow for Query and Feedback :)
0 comments:
Post a Comment