【问题标题】:How to write in the same file C++如何在同一个文件中写入C++
【发布时间】:2019-10-13 15:34:39
【问题描述】:

我需要删除文件中所有出现的字符串。 我将文本作为字符串接收并删除所有出现的事件。 在我删除了所有出现后,我不知道如何将字符串保存回文件。

我尝试关闭文件并使用 ofstream 进行写入,但没有成功。

#include <iostream>
#include <fstream>
#include <string>

int main () {
    std::string file_contents = "";
    std::ifstream myfile ("text.txt");

    char ch;

    if (myfile.is_open())
    {
        // READ FILE CONTENTS AS STRING
        while ( myfile >> std::noskipws >> ch)
        {
            file_contents += ch;
        }

        // DISPLAY STRING
        std::cout << file_contents << '\n';

        // GET WORD TO BE DELETED
        std::string word;
        std::cout << "Please enter word to be deleted: ";
        std::cin >> word;

        std::string::size_type found;

        std::string new_text;
        //DELETE WORD FROM STRING
        bool ok=0;
        do{
        found = file_contents.find(word);
        ok=1;
        if (found!=std::string::npos)
        {
            std::cout << word << " found at: " << found << '\n';
            file_contents.erase(found, word.length());
            std::cout << file_contents << '\n';

        }
        else
            ok==0;
            new_text=file_contents;
        }while(ok==1);



        myfile.close();
    }

    else std::cout << "Unable to open file";

    return 0;
}

【问题讨论】:

  • ifstream 是输入文件流,不能往文件里放东西。为什么不关闭 ifstream,从检索到的内容中删除单词,然后使用 ofstream 打开同一个文件并写入所有内容?
  • 请出示minimal reproducible example,您说您尝试使用ofstream 但它不起作用,是什么原因不起作用?这个代码在哪里?
  • 我试过了,但没用。文件保持不变
  • 旁注:请使用truefalse 作为bool 变量。
  • @user9888273 我添加了一个有效的答案。

标签: c++ string file


【解决方案1】:

好的,因此您必须先关闭 ifstream 实例,然后再继续写入文件。

关闭文件后,修改内容,然后使用ofstream打开同一个文件进行写入,简单地写入内容。

#include <iostream>
#include <fstream>
#include <string>

int main() {
  std::string file_contents = "";
  std::ifstream myfile("text.txt");

  char ch;

  if (myfile.is_open())
  {
    // READ FILE CONTENTS AS STRING
    while (myfile >> std::noskipws >> ch)
    {
      file_contents += ch;
    }
    myfile.close();
  }
  else {
    std::cout << "Unable to open file";
    return -1; // no need to continue if can't read it
  }

  // DISPLAY STRING
  std::cout << file_contents << '\n';

  // GET WORD TO BE DELETED
  std::string word;
  std::cout << "Please enter word to be deleted: ";
  std::cin >> word;

  //DELETE WORD FROM STRING
  size_t found;
  while ((found = file_contents.find(word)) != file_contents.npos)
  {
    std::cout << word << " found at: " << found << '\n';
    file_contents.erase(found, word.length());
    std::cout << file_contents << '\n';
  }

  // this will open in text mode and will replace all existing content
  std::ofstream out("text.txt"); 
  if (out.is_open()) {
    out << file_contents;
    out.close();
  }
  else {
    std::cout << "Unable to open file for writing.\n";
    return -2; // for failure to open for write
  }
  return 0;
}

注意:当我试图执行它时,你已经无限循环了,我必须将它更改为上面显示的代码。还有,new_text 完全没必要,为什么要有呢?

【讨论】:

    【解决方案2】:

    最简单的方法是先作为输入流打开。当完成打开作为输出流写入。这不是你能做到这一点的唯一方法。

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main ()
    {
        std::string file_contents = "";
        {
            std::ifstream myfile ("text.txt");
    
            if (! myfile.is_open()) 
            {
                else std::cout << "Unable to open file";
                return 1;
            }
    
            // READ FILE CONTENTS AS STRING
            char ch;
            while ( myfile >> std::noskipws >> ch) file_contents += ch;
            myfile.close();
        }
    
    
        {
            std::string word;
            std::cin >> word; // GET WORD TO BE DELETED
            std::string::size_type found;
            while((found = file_contents.find(word))!=std::string::npos)
                file_contents.erase(found, word.length());
    
            std::ofstream myfile ("text.txt");
            myfile << file_contents<< std::flush;
            myfile.close();
        }
    
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      相关资源
      最近更新 更多