Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:00:53+00:00 2026-06-11T14:00:53+00:00

Below i have a code that reads a text file and only writes a

  • 0

Below i have a code that reads a text file and only writes a line to another textfile if it has the word "unique_chars" in it. I also have other garbage on that line such as for example. "column" How can i make it replace the phrase "column" with something else such as "wall"?

So my line would be like <column name="unique_chars">x22k7c67</column>

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

    ifstream  stream1("source2.txt");
    string line ;
    ofstream stream2("target2.txt");

        while( std::getline( stream1, line ) )
        {
            if(line.find("unique_chars") != string::npos){
             stream2 << line << endl;
                cout << line << endl;
            }

        }


    stream1.close();
    stream2.close();    

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:00:54+00:00Added an answer on June 11, 2026 at 2:00 pm

    To do the substitution you can use std::string’s method “replace”, it requires a start and end position and the string/token that will take the place of what you’re removing like so:

    (Also you forgot include the string header in your code)

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream  stream1("source2.txt");
        string line;
        ofstream stream2("target2.txt");
    
        while(getline( stream1, line ))
        {
            if(line.find("unique_chars") != string::npos)
            {
                string token("column ");
                string newToken("wall ");
                int pos = line.find(token);
    
                line = line.replace(pos, pos + token.length(), newToken);
                stream2 << line << endl;
                cout << line << endl;
            }
        }
    
        stream1.close();
        stream2.close();    
    
        system("pause");
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following code that reads a text file line by line.
I have a piece of code (below) that can get the text of an
I have a text editor that uses code almost identical to the below for
I have an XML file that is pretty long. Below is the code I
below i have a code that runs in most of my simple programs ..
I have code that looks more or less like the code below but it
I have the code below that hides and shows the navigational bar. It is
I have this javascript code below that uses jquery, it is suppoed to be
I have the code below on form so that the user submits the form
I have the code below. I'm wondering that does the self.value and _value have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.