473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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"<<site;
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),site.length()," ");
out.close();
return 0;
}
case default:
cout<<"wrong choice dude run exe again";
}return 0;
}
Attached Files
File Type: txt myprog.txt (1,012 Bytes, 314 views)
Jul 10 '14 #1
6 1417
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 10 '14 #2
donbock
2,426 Expert 2GB
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' ?
Jul 10 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 10 '14 #4
donbock
2,426 Expert 2GB
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'?
Jul 11 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 12 '14 #6
thnx for the conversation but i already found the solution
Jul 12 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cpptutor2000 | last post by:
Could some C guru help me please? I am using the following program to open a SSH connection to a remote host and eventually run a program on that remote host. #include <stdio.h> #include...
36
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): ...
3
by: krishnakant Mane | last post by:
hello all. I have one simple query and may be that's to stupid to answer but I am not finding the answer any ways. I have a set of modules in my package and out if which one is my actual starting...
9
by: ehab mohamed | last post by:
#include<stdio.h> void num(char arr) { int i=0, count=1; while (arr!='\0') { if (arr==' ') {count++;} i++; }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.