【问题标题】:saving the changes in file保存文件中的更改
【发布时间】:2012-03-09 13:49:39
【问题描述】:

目标:要打开配置文件,查找具有标签的特定行并更改其值

示例:

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

到目前为止我所做的: 我的代码打开文件并查找具有标记的特定行并更改其值。

问题:我没有在实际文件中看到这些更改。我知道 ofstream 将写入行,但如果我使用 ofstream 而不是 ifstream getline 函数会出错。

代码1:

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:也不行。

 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"));
}

【问题讨论】:

  • ifstream = 输入文件流,ofstream = 输出文件流。如果你想要两者:fstream。
  • 我尝试过使用 fstream 但在这种情况下 getline 函数不起作用。在发布之前,我添加了一些调试语句来检查它
  • 它不允许粘贴配置文件的所有内容。配置文件的文件权限为 777,以防万一

标签: c++


【解决方案1】:

您所做的事情本身就很棘手,写入文件的中间希望结果保持不变。

例如

192.168.120.111 

使用的字符比

多很多
10.10.10.10

我这样做的方法是将整个文件读入行。

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"));

请注意,您不需要在 C++ 中显式关闭文件,RAII 语义会为您关闭它,您可以自行关闭它,从而增加错误潜入代码的机会。

这适用于我的系统

#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;
}

【讨论】:

  • @1111111 ,我仍然面临同样的问题。它转到 while 语句,但其中的 temp 为空。
  • 好吧,在我的例子中,上面的函数甚至没有从 getline 读取行。我在 while 之后添加了一个调试语句来检查 temp 中的内容,但它似乎是空的。我在这里做错了吗?
  • 听起来你正在读取错误的文件或类似的东西
  • 我不这么认为,我已经检查了很多次以确保我正在阅读正确的文件
  • 您可以将确切的文件添加到您的问题中吗?
【解决方案2】:

通过使用 ifstream 打开文件进行读取,如果您使用 fstream 基类代替,您应该能够修改文件

另一种方法是将整个文件读入缓冲区,对缓冲区进行更改并使用 ofstream 类覆盖原始文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2014-04-06
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多