用 C++ 程序附加一个文本文件内容到另一个文本文件
這是一個用 C++ 編寫的程式,用來附加某個文字檔案的內容到另一個文字檔案。
輸入
a.txt file contains “Tutorials” a1.txt file contains “point”
輸出
Tutorialspoint
演算法
Begin Define a fstream class object as fin. Open a input file a.txt with input file stream class object fin. Open a output file a1.txt with output file stream class object fout in append mode. Check if the file is not existing then Print “File not found”. Else append content from fin to fout. Open the destination file in read mode. Display its content as output. End.
範例程式碼
#include <bits/stdc++.h> #include<fstream> using namespace std; int main() { fstream f; ifstream fin; fin.open("a.txt"); ofstream fout; fout.open("a1.txt", ios::app); if (!fin.is_open()) { cout << "Tutorialspoint"; } else { fout << fin.rdbuf(); } string word; f.open("a1.txt"); while (f >> word) { cout << word << " "; } return 0; }
輸出
Tutorialspoint
广告