【问题标题】:Is fstream the proper way to edit a textfile with c++?fstream 是用 c++ 编辑文本文件的正确方法吗?
【发布时间】:2018-04-04 21:58:42
【问题描述】:

我想编辑一个包含多行的文本文件。在每一行中,如果第一个字符是空格,我想删除它。

我找到了多个使用 ifstream 和 ofstream 的示例。但是在那里,输入和输出文件是不同的文件。

但我希望输入和输出是同一个文件,但我不知道 fstream 是否以及如何工作。

    fstream file;
    file.open(path, ios::in | ios::out);

     while (getline(file, line))
     {
           if (line[0] == ' ')
           {
                line.erase(0, 1);
           }
           file << line << "\n";
     }

到目前为止,代码并没有改变我文件中的任何内容。

感谢您的帮助。

【问题讨论】:

  • 编辑文件就地可能是一项微妙的工作,考虑读入整个文件(可能读入字符串向量),修改它,然后将其写出。
  • 另一个提示:将程序和操作系统的功能结合起来通常很方便。虽然例如gnu sed 有一个 --inplace 选项,我已经很好地使用了它,使用标准 sed 只是写入标准输出并在操作系统的帮助下重定向输出并不罕见.当一个人对结果感到满意时,就可以简单地用新文件覆盖旧文件。例如。 sed 's/^ .*//g' some.txt &gt; tmp.txt &amp;&amp; mv tmp.txt file.txt。原因是 OS shell 已经很好地完成了文件处理,而且写入标准输出的功能出奇地多。
  • 关于手头的主题(这是对早期帖子的更正):目前您根本没有回写文件。您可能希望将文件存储在内存中(使用@vu1p3n0x 的建议),对其进行修改,然后将其写回磁盘上的文件。我想重写文件的最简单方法是关闭输入流并使用trunc 选项重新打开它以进行写入,正如here 所建议的那样。我之前使用seekg 的建议不会截断文件,而只会覆盖开头(你的行数比原来的少)。
  • 如果您想要一个可移植的解决方案,那么您应该使用两个文件(或一个临时文件)。如果没有,您可以使用fstream::seekp 将自己定位到您想要的位置并覆盖文件内容。
  • @PeterA.Schneider 最简单的方法是最危险的。如果您的程序崩溃,您将丢失文件。安全的方法是保存到其他文件,删除原始文件,然后重命名新文件。

标签: c++


【解决方案1】:

你可以用两个文件来做:

// TODO: error checking!
fstream file1;
file1.open(path1, ios::in);
fstream file2;
file2.open(path2, ios::out | ios::trunc);

 while (getline(file1, line))
 {
       if ( !line.empty() && line[0] == ' ' )
             file2 << line.substr( 1 ) << "\n";
       else
             file2 << line << "\n";
 }

 file2.close();
 file1.close();

或使用ostringstream

// TODO: error checking!
fstream file;
file.open(path, ios::in);
ostringstream osstream;

 while ( getline( file, line ) )
 {
       if ( !line.empty() && line[0] == ' ' )
             osstream << line.substr( 1 ) << "\n";
       else
             osstream << line << "\n";
 }

 file.close();
 file.open( path, ios::out | ios::trunc );
 file << osstream.str();
 file.close();

【讨论】:

  • 但是提问者想用一个文件来做:“但我希望输入和输出是同一个文件”
  • 我想根本无法保证该行中有一个字符。
【解决方案2】:

到目前为止,代码并没有改变我文件中的任何内容。

您的问题是您正在修改该行(我认为这只是一个字符串),但没有将其写回文件

【讨论】:

    【解决方案3】:

    感谢您的帮助!我已按照 vu1p3n0x 的建议将所有内容保存在向量中。我还删除了命名空间(以防万一),我也会在我未来的程序中这样做。我从来没有考虑过,多亏了 tambre。

    我认为代码远非完美,但在这里以防有人需要它。

                std::ifstream filein;
                std::string line;
                filein.open(help);
                std::vector<std::string> temp;
    
                while (std::getline(filein, line))
                {
                    if (!line.empty())
                    {
                        if (line[0] == ' ')
                        {
                            line.erase(0, 1);
                        }
                    }
                    temp.push_back(line);
                }
                filein.close();
    
                std::ofstream fileout;
                fileout.open(help, std::ofstream::out, std::ofstream::trunc);
    
                int l = 0;
                while (l < temp.size())
                {
                    //cout << temp[l] << "\n";
                    fileout << temp[l] << "\n";
                    l++;
                }
                fileout.close();
    

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2017-04-24
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多