// istream get
#include <iostream>
#include <fstream>
#include<string>
#include<cstring>
//#include<sstream>
using namespace std;

int main () {
char c, str[256];
ifstream is;

cout << "Enter the name of an existing text file: ";
cin.get (str,256);

is.open (str); // open file

ofstream was;
was.open("output.mvw");

string st ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;

// different member versions of find in the same order as above:
found=st.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;
// let's replace the first needle:
st.replace(st.find(str2),str2.length(),"preposition");
cout << st << endl;
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
if (is.good())
was << c;
}

is.close(); // close file
was.close();
return 0;
}


But I am not getting output. Means actually even if I am trying with simple program also

like 

#include<iostream>
using namespace std;

int main()
{
int i;
cout<<"Enter a number";
cin>>i;
cout<<"The number is "<<i;
return 0;
}

Dear Sir,

I have one input file say following

input.txt

Shridhar Vishvanath Suryawanshi
Radha Mahima Suryawanshi

Now I want an output file say output.txt with the following changes

output.txt

Shridhar Vishvanath Suryawanshi
Radha Shridhar Suryawanshi

So I want to replace and insert Shridhar at the place of Mahima in input.txt file and write it in output file with the changes and as output.txt

I can do above things separately means I can read file and write it as output.txt

Also I can replace word Mahima with Shridhar Separately

But I can't read the word Mahima from input.txt and replace it with Shridhar and write it in output.txt file.

In short I want to find and replace words from a file and write them in output.txt file

Thank u

Recommended Answers

All 8 Replies

PLEASE IGNORE FOLOOWING PART FROM MY ABOVE THREAD


But I am not getting output. Means actually even if I am trying with simple program also

like

#include<iostream>
using namespace std;

int main()
{
int i;
cout<<"Enter a number";
cin>>i;
cout<<"The number is "<<i;
return 0;

Dear Sir,
I am waiting for my above query.
I am trying to replace and insert words in text file and create a new text file .

I am trying with above example in my previous thread.

Thank u

In your simple program, is it displaying the "Enter a number" prompt?

Putting an "endl" after a cout statement puts in a newline and also flushes the output buffer. I believe you can simply put a "flush" if you don't want the newline.

Dear Sir,
My query is actually as guven in the following program.

I am trying to find, replace words or strinrs from an input file and create an output.txt file with the cahnges.

Say suppose

input.txt
My name is Suman
My son's name is somnath.

Now I want to replace word somnath on the second line with shridhar and create
output file as
output.txt
My name is Suman
My son's name is shridhar.

This I am trying with the following program.
But I can read file and write file as it is.
Also I can find and replace word from a string or sentence.

But how to find and replace words from input.txt and put it in output.txt

#include <iostream>
#include <fstream>
#include<string>
#include<cstring>
//#include<sstream>
using namespace std;

int main () {
char c, str[256];
ifstream is;

cout << "Enter the name of an existing text file: ";
cin.get (str,256);

is.open (str); // open file

ofstream was;
was.open("output.mvw");

string st ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;

// different member versions of find in the same order as above:
found=st.find(str2);
if (found!=string::npos)
cout << "first 'needle' found at: " << int(found) << endl;
// let's replace the first needle:
st.replace(st.find(str2),str2.length(),"preposition");
cout << st << endl;
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
if (is.good())
was << c;
}

is.close(); // close file
was.close();
return 0;
}

Thanks

Use getline to read the lines from the input file into a temporary string:
Read: "My name is Suman" as a std::string
Does this contain "somnath"? No, so write directly to the output file

Read:"My son's name is somnath." as a std::string
Does this contain "somnath"? Yes it does. Replace somnath with "shridhar" (perhaps using the replace method of the string).
Once this change is made, write the temporary string out to the file.

Obviously this will need to be in a loop to get all of the strings in the input file.

Give it a try based on that, and post back with the code (and please use code tags: type in [code] //code goes here [/code])

Dear Sir,

I am putting my code with my best effort to change words in a file and write the replaced word in output file.

But I am not getting output.

Actullay I tried to read file line by line.
Then I suppose wanted to cahnge word on line number two.
So I put if(i=2) and then tried to replace the word.

Plase check my code and tell me if anything else required to be done.

Because I am not geeting output file with the following changes

//code 
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int main()
{
        
    
    
    string str2="shridhar";
    
    ifstream infile;
    infile.open("radha.txt");
    
    ofstream outfile;
    outfile.open("shridhar.txt");
    
    size_t found;
    
    int size = 0;
    string line;
    if(infile.is_open())
    {
         while((getline(infile,line)))
                    {
                                      if(size=1)
                                      {
                                             int found=line.find(str2);
                                             if (found!=string::npos)
                                             {
                                             line.replace(line.find(str2),str2.length(),"swapnil");
                                             }
                                             }    
                        outfile<<line<<endl;
                      }
                        size++;
      }

infile.close();
    outfile.close();
    
    system("pause");
    
    return 0;
    
}

)

if(i=2) Does that statement look correct to you? You need to be testing for equality (==) and not assigning (=). I don't think that's the entire issue, but start there.

Plase check my code and tell me if anything else required to be done.

Add error checking. How do you know the input file was opened? How do you know the input file was read?

And please fix your formatting. Your code is hard to follow with your inconsistent indentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.