December 29, 2011

C++ Program Unblocks a Website

In our previous tutorial, we presented you a C++ program that could block a web site on your computer through employing slight changes in the hosts file. read it here, C++ Program to Block a Website
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:

/* C++ Program Unblocks a website ~ MyCFiles.com */
#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;
}

Sample Output:
As in previous article we blocked fb here we are unblocking it

Code Analysis:
The logic of the program is simple. We input the web site name from the user and store it on an array. In the hosts file, we try to take a line and store its content in the array and then simply perform an string matching algorithm to find out that whether site name is included in the line array taken out of file. If we get affirmative result, we note out the number of line. Later, in the program, we open the file in the write mode and go to the stored line number and change all the contents of that line to white spaces. So, here is the program.

Also Read : Block any Website on Computer using C Code


Hope you like the post, comment bellow for Query and Feedback :)

Related Posts :



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails