如何在 C++ 中将文本附加到文本文件?


这是一个将文本附加到文本文件中的 C++ 程序。

算法

Begin
   Open that file a1.txt as output file stream class object to perform
   output operation in append mode using fout file reference.
   If the file exists then
      Appending text to that file.
   Close fout.
   Open the file “a1.txt” for reading the content of the file.
   Extracting the text from file and printing the text.
End.

示例代码

 在线演示

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
   fstream f;
   ofstream fout;
   ifstream fin;
      fin.open("a1.txt");
      fout.open ("a1.txt",ios::app);
   if(fin.is_open())
      fout<<" tutorials point";
      cout<<"\n Data has been appended to file"<<endl;
      fin.close();
      fout.close();
      string word;
      f.open("a1.txt");
      while (f >> word) {
         cout << word << " ";
      }
      return 0;
}

输出

Data has been appended to file

更新于: 2019 年 7 月 30 日

7K+ 查看

开启你的 职业

完成课程获得认证

开始
广告
© . All rights reserved.