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 8012025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:12:12+00:00 2026-06-04T19:12:12+00:00

I am looking for some advice. My situation: Application works with text local file

  • 0

I am looking for some advice.

My situation:

  • Application works with text local file.

  • In file are somewhere tags like this:

    correct = "TEXT"

    . Unfortunatelly, there can be unlimited spaces between correct, = and “TEXT”.

  • Obtained text is testing in function and may be replaced (the change must be stored in the file).

     correct = "CORRECT_TEXT"

My current theoretical approach:

  • With ofstream — read by line to string.

  • Find tag and make change in string.

  • Save strings as lines to the file.


Is there some simplify way (with iterators?) in C++ with using standard system libraries only (unix).

Thank you for your ideas.

  • 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-04T19:12:13+00:00Added an answer on June 4, 2026 at 7:12 pm

    Here is a possible solution that uses:

    • std::getline()
    • std::copy()
    • istream_iterator
    • ostream_iterator
    • vector

    Example:

    #include <iostream>
    #include <fstream>
    #include <iterator>
    #include <algorithm>
    #include <string>
    #include <vector>
    
    struct modified_line
    {
        std::string value;
        operator std::string() const { return value; }
    };
    std::istream& operator>>(std::istream& a_in, modified_line& a_line)
    {
        std::string local_line;
        if (std::getline(a_in, local_line))
        {
            // Modify 'local_line' if necessary
            // and then assign to argument.
            //
            a_line.value = local_line;
        }
        return a_in;
    }
    
    int main() 
    {
        std::ifstream in("file.txt");
    
        if (in.is_open())
        {
            // Load into a vector, modifying as they are read.
            //
            std::vector<std::string> modified_lines;
            std::copy(std::istream_iterator<modified_line>(in),
                      std::istream_iterator<modified_line>(),
                      std::back_inserter(modified_lines));
            in.close();
    
            // Overwrite.
            std::ofstream out("file.txt");
            if (out.is_open())
            {
                std::copy(modified_lines.begin(),
                          modified_lines.end(),
                          std::ostream_iterator<std::string>(out, "\n"));
            }
        }
    
        return 0;
    }
    

    I am not sure exactly what the manipulation of the lines should be but you could use:

    • std::string::find() and std::string::substr()
    • boost::split()

    EDIT:

    To avoid storing every line in memory at once the initial copy() can changed to write to an alternative file, followed by a file rename():

    std::ifstream in("file.txt");
    std::ofstream out("file.txt.tmp");
    
    if (in.is_open() && out.open())
    {
        std::copy(std::istream_iterator<modified_line>(in),
                  std::istream_iterator<modified_line>(),
                  std::ostream_iterator<std::string>(out, "\n"));
    
        // close for rename.
        in.close();
        out.close();
    
        // #include <cstdio>
        if (0 != std::rename("file.txt.tmp", "file.txt"))
        {
            // Handle failure.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into building a distributed application and would like some advice on the
Looking for some advice on whether my syntax is correct for this Prototype to
I was looking for some advice on building a Web Framework. I'd like to
Just looking for some advice on this one, what exactly is a loading screen
So I've seen this question , but I'm looking for some more general advice:
Looking for some advice on the best way to implement localization along with client
I'm looking for some advice. My boss is writing a series of lengthy articles.
I am looking for some advice on how to allow easy customisation and extension
I'm looking for some advice on the best way to provide a single place
I'm looking for some advice on whether or not I should use a separate

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.