如何在c ++中与另一行交换 [英] how to swap one line with another in c++

查看:109
本文介绍了如何在c ++中与另一行交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是搜索文件中的特定行,修改它并将文件保存在c ++中。我试图通过fstream这样做如下所示

My objective is to search for a particular line in the file, modify it and save the file in c++. i am trying to do this via fstream such as shown below

fstream someFile(fileName, filestream::ate | filestream::in |  filestream::out);
streampos endPos =  someFile.tellg();
streampos.seekg(0, fstream::beg);
string line;
while(someFile && someFile.tellg() != endPos  && getline(somefile, line))
{
  if(correctLine(line))
  {
    modifyLine(line);
   //save Line.. How do i do this?
  }
 }

我的问题是如何替换getline函数与修改行?
此外,在getline之后,文件位置将在当前行的开头还是放在当前行的末尾?

My question is how do i replace the line obtained from getline function with the modified line? Also, after getline, would the file position will be at the start of the current line or would it be placed at the end of the current line?

推荐答案

@harper是正确的,你不能只是改变文件,除非新行与旧行的大小相同。您可以执行的操作是读入 stringstream ,然后写出 stringstream 退出,用新版本完全覆盖该文件。

@harper is correct, that you can't just alter the file unless the new line is the same size as the old line. What you can do is read into a stringstream and then write the stringstream back out, completely overwriting the file with the new version.

另一个问题替换单个行的示例。在你的情况下,你只需要做比 line == target 更复杂的事情,但它基本上是一样的。

Here's an example of replacing a single line from another question. In your case, you'd just need to do something more complicated than the line == target, but it's basically the same.

请注意,这会将整个文件读入内存,因此如果文件很大,将使用大量内存。

Be warned that this reads the entire file into memory, so it will use a lot of memory if the file is large.

#include <fstream>
#include <iostream>
#include <sstream>

int main(int argc, char** argv) {

    /* Accept filename, target and replacement string from arguments for a more
       useful example. */
    if (argc != 4) {
        std::cout << argv[0] << " [file] [target string] [replacement string]\n"
            << "    Replaces [target string] with [replacement string] in [file]" << std::endl;
        return 1;
    }

    /* Give these arguments more meaningful names. */
    const char* filename = argv[1];
    std::string target(argv[2]);
    std::string replacement(argv[3]);

    /* Read the whole file into a stringstream. */
    std::stringstream buffer;
    std::fstream file(filename, std::fstream::in);
    for (std::string line; getline(file, line); ) {
        /* Do the replacement while we read the file. */
        if (line == target) {
            buffer << replacement;
        } else {
            buffer << line;
        }
        buffer << std::endl;
    }
    file.close();

    /* Write the whole stringstream back to the file */
    file.open(filename, std::fstream::out);
    file << buffer.str();
    file.close();
}

注意:如果你想要聪明,直到找到要替换的行,然后只替换之后的内容。

Note: If you want to be clever, you could probably leave the file alone until you find the line to replace, and then only replace the content after that..

这篇关于如何在c ++中与另一行交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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