C ++ - 在文本文件(标准系统库)中查找和替换 [英] C++ - Find and replace in text file (standard system libraries)

查看:280
本文介绍了C ++ - 在文本文件(标准系统库)中查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一些建议。



我的情况:




  • 应用程序使用文本本地文件


  •  correct =TEXT

    。不正确,正确 = TEXT之间可以有无限的空格。


  • 获取的文本是在函数中测试的,可以替换(更改必须存储在文件中)。

     correct =CORRECT_TEXT




理论方法:




  • 使用 ofstream / p>


  • 在字符串中查找标签并进行更改。







有一些简单的方法



感谢您的想法。

解决方案

以下是一种可能的解决方案:





示例:

  #include< iostream& 
#include< fstream>
#include #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))
{
//必要时修改'local_line'
//然后赋给参数。
//
a_line.value = local_line;
}
return a_in;
}

int main()
{
std :: ifstream in(file.txt);

if(in.is_open())
{
//加载到向量中,在读取时进行修改。
//
std :: vector< std :: string> modified_lines;
std :: copy(std :: istream_iterator< modified_line>(in),
std :: istream_iterator< modified_line>(),
std :: back_inserter
in.close();

//覆盖。
std :: ofstream out(file.txt);
if(out.is_open())
{
std :: copy(modified_lines.begin(),
modified_lines.end(),
std :: ostream_iterator ; std :: string>(out,\\\
));
}
}

return 0;
}

我不知道行的操作应该是什么,使用:





编辑:



为了避免在内存中同时存储每一行​​,初始 copy()可以更改为写入备用文件,后跟文件 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>
std :: istream_iterator< modified_line>(),
std :: ostream_iterator< std :: string>(out,\\\
));

//关闭重命名。
in.close();
out.close();

// #include< cstdio>
if(0!= std :: rename(file.txt.tmp,file.txt))
{
//处理失败。
}
}


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.

解决方案

Here is a possible solution that uses:

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:

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.
    }
}

这篇关于C ++ - 在文本文件(标准系统库)中查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆