program to add/ remove entry in host is not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pranjalgoenka
    New Member
    • Jul 2014
    • 3

    program to add/ remove entry in host is not working

    the first part of theprogram is working on adding host but an error is generating again and again that search is not a function of 'ofstream'.

    plz help me out

    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    int main()
    {
    char site[20],c,d;
    ifstream in; ofstream out;
    cout<<"press 1 to block website & press 2 to open website";
    cin>>d;
    switch(d)
    {
    case 1:
    {
    cout<<"Enter the Name of the Site to Block \n";
    cin>>site;
    out.open("C:/Windows/System32/drivers/etc/hosts",ios::app );
    if(!out)
    cout<<"Either File Not Found or Permission Denied, Run as Admin the EXE of the Program";
    else
    out<<"127.0.0.1 "<<"\t"<<si te;
    out.close();
    return 0;
    }
    case 2:
    {
    cout<<"Enter the Name of Website to open \n";
    cin>>site;
    out.open("C:/Windows/System32/drivers/etc/hosts",ios::app );
    if(!out)
    cout<<"Either File Not Found or Permission Denied, Run as Admin the EXE of the Program";
    else
    out.replace(out .find(site),sit e.length()," ");
    out.close();
    return 0;
    }
    case default:
    cout<<"wrong choice dude run exe again";
    }return 0;
    }
    Attached Files
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    out.replace(out.find(site),site.length(),"      ");
    find and replace are not member functions of an output stream. Output data can't be searched because the data has left the program.

    To search you need to have the data in a container and then you can search the container.

    If you review the member functions for the ofstream class, you will not see find and replace mentioned.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I don't use C++ so this question may not make sense ...

      When you cin into a char, do you get a character value or an integer value? That is, should it be case 1 or case '1' ?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Case 1 is the integer 1. Case '1' is the integer for the character 1. In the ASCII table '1' is 49. Therefore, case '1' and case 49 are equivalent.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          What I meant to ask was...
          1. If you cin into a char variable,
          2. and if you press the "1" key on the keyboard,
          3. does the variable get set to integer value of 1 or character value of '1'?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            cin >> to a char will put 49 in the char (the same as '1').

            cin >> to an int will put binary 1 in the int.

            The char is supposed to contain ASCII characters. You can out a binary 1 in the char by assignment but you can't do it using a keyboard.

            Comment

            • pranjalgoenka
              New Member
              • Jul 2014
              • 3

              #7
              thnx for the conversation but i already found the solution

              Comment

              Working...