censoring a word

Apr 9, 2012 at 12:28pm
Hello.
I need to write a program that reads from a text file that has a couple words(5-6) and creates another text file that has all thease words except a word that i want to censor

for example
test1.text has 6 words(one word per line). i will have c++ to asking me the word i want to be censored.
Then c++ will make me another text file (test2.txt) with all the words from test1.txt except the word i chose to censor and it should be replaced with

Apr 9, 2012 at 12:43pm
Which one do you not know how to do?
* read from a file
* write to a file
* compare strings for equality
* write an if statement
Apr 9, 2012 at 1:52pm
reading from a file and writing to a file i understand so comparing strings and the if statement is the part i don't get.



Apr 9, 2012 at 3:06pm
this is what i have written



#include <iostream>
#include <fstream>
using namespace std;

int main(char *arg[])
{




ifstream filename;

filename.open("test1.txt");

if(!filename.is_open())
{
cout<<"cannot open file";
return 0;
}


char word[50];
filename>>word;
while(filename.good())
{
cout<<word<<" ";
filename>> word;
string rida, valjundrida;
getline(filename,rida);
}
cout<<"\n"<<"Enter the word you want to censor "<<endl;
string censor;
cin>>censor;



string file2("test2.txt");
ofstream out(test2.c_str());




}
Last edited on Apr 9, 2012 at 3:06pm
Apr 9, 2012 at 5:51pm
If you use std::string, comparison is just ==. If you use char arrays instead, you need to use strcmp() from <cstring>. strcmp will return 0 if the strings are equal.

Here's pseudocode:
open input file, open output file.

while there is something in the input file
   read string S from it
   if S is equal to the one you want to censor then
      write the replacement string to the output file
   else
      write S to output file
Apr 9, 2012 at 10:15pm
Thank you.
Topic archived. No new replies allowed.