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

  • Home
  • SEARCH
  • 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 7607589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:42:09+00:00 2026-05-31T00:42:09+00:00

Ojective: To open config file, look for a particular line which has tag and

  • 0

Ojective: To open config file, look for a particular line which has tag and change its value

Example:

    config .txt    
    bla bla    
    bla bla   
    SYSTEM_A_EXTERNAL_IP=192.168.0.57  // need to change ip address    
    bla bla   
    bla bla 

What I have done so far: My code open the file and look for the particular line which has tag and change its value .

Problem: I failed to see those changes in the actual file. I am aware that ofstream will write into line but if I do ofstream instead of ifstream getline function gets an error.

Code1:

bool SetIpAddr(string & iAddr)    
{     
    bool check= false;    

    // Open  file    
    ifstream  iFile(IpConfigFile.c_str(), ios_base::out|ios::app | ios::binary);
    if(iFile.is_open())    
    {    
        check= true;    
    }          

    // Read content of ipconf file searching for the value of Head Id Name
    if(true == check)
    {
        string TargetName = SystemAIdValue + ExternalIpNamePostfix+ ValueAssignmentCharacter;
        string outbuf;
        while( !iFile.eof() )
        {
            getline(iFile, outbuf);    

            // find the matching string     
            if( 0 == outbuf.compare(0,TargetName.size(),TargetName) )
            {
            outbuf.erase(outbuf.begin()+TargetName.size(), outbuf.end());    
            outbuf.insert(0+TargetName.size(), iAddr);
                std::cout<< " after insertion  " << outbuf << std::endl;
                break;
            }
        }
    }
    // Close  (input) file
    if( iFile.is_open() )
    {
        iFile.close();
    }
    return check; 
}

Code2: Also doesnt work.

 bool SetIpAddr(string & aIpAddr)
 {
     bool lResult = false;    
     std::fstream  ifile("platform_ip_config");      
     string lTargetIp = HeadAIdValue + ExternalIpNamePostfix+ ValueAssignmentCharacter;
     std::deque<std::string>lines;
     std::string inbuf;
     while( std::getline(ifile, inbuf));
     {
         std::cout<<" we are in while"<<std::endl;
         std::cout<<" getline =="<< inbuf<< std::endl;
         std::cout<<" ip =="<<lTargetIp<< std::endl;
         if( 0 == inbuf.compare(0,lTargetIp.size(),lTargetIp) )
         {
             std::cout<<" we are in matching"<<std::endl;            
             std::cout<< inbuf << std::endl;
         inbuf.erase(inbuf.begin()+lTargetIp.size(), inbuf.end());
             std::cout<<" after erase " << inbuf << std::endl;
         inbuf.insert(0+lTargetIp.size(), aIpAddr);
             std::cout<< " after insertinon  " << inbuf << std::endl;        
         }
         lines.push_back(inbuf);
     } 
     ifile.seekg (0, ios::beg );
     std::copy(lines.begin(), lines.end(), 
     std::ostream_iterator<std::string>(ifile, "\n"));
  }
  lines.push_back(inbuf);
}
ifile.seekg (0, ios::beg );
std::copy(lines.begin(), lines.end(), 
std::ostream_iterator<std::string>(ifile, "\n"));
}
  • 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-05-31T00:42:10+00:00Added an answer on May 31, 2026 at 12:42 am

    What you are doing is inherently tricky, writing to the middle of a file hoping the result will remain in place.

    for example

    192.168.120.111 
    

    uses up a lot more characters than

    10.10.10.10
    

    the way i would do this is read the whole files into lines.

    std::fstream file(file_name);
    std::deque<std::string> lines;
    std::string temp;
    while(std::getline(file, temp))
    {
        //detect that this the line you want
        if(this_is_my_line(temp)) 
        {
             //do what you need to do to the string
             modify(temp);
        }
    
        lines.push_back(temp);
    }
    file.seekg (0, ios::beg
    //write back to file
    std::copy(lines.begin(), lines.end(), 
              std::ostream_iterator<std::string>(file, "\n"));
    

    Please note you do not need to explicitly close a file in C++, the RAII semantics will close it for you, by doing it your self your increasing the opportunity for bugs to creep into your code.

    THIS WORKS ON MY SYSTEM

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <deque>
    #include <iterator>
    int main()
    {
        std::string line;
        const std::string token("SYSTEM_A_EXTERNAL_IP=");
        std::deque<std::string> lines;
        {
            std::ifstream file("config.txt");
            while(std::getline(file, line))
            {
                if(std::equal(token.begin(), token.end(), line.begin()))
                {
                    line=token+"10.10.10.5"; //or whatever
                }
                lines.push_back(line);
            }
        }
        {
            std::ofstream file("config.txt");
            std::copy(lines.begin(), lines.end(),
                 std::ostream_iterator<std::string>(file,"\n"));
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a very basic C snippet to open and read a file: int fd
The Objective To read the csv file, and separate each line into an array.
In an objective-c/cocoa app, I am using c functions to open a text file,
When I open the new file dialog I see only 3 Cocoa Touch Classes:
Mayb do you know a good open-source pdf renderer for objective-c? :)
In objective-c I can do the following: NSTask* foo = [NSTask alloc]init]; [foo setLaunchPath:@/usr/bin/open];
Objective-C has no namespaces; it's much like C, everything is within one global namespace.
Objective: take a UIImage, crop out a square in the middle, change size of
Objective: My script will download a remote file upon form submission, since the file
Objective Change these filenames: F00001-0708-RG-biasliuyda F00001-0708-CS-akgdlaul F00001-0708-VF-hioulgigl to these filenames: F0001-0708-RG-biasliuyda F0001-0708-CS-akgdlaul F0001-0708-VF-hioulgigl Shell

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.