给定源和目标文本文件。将源文件的内容附加到目标文件,然后显示目标文件的内容。
null
例子:
Input : file.txt : "geeks", file2.txt : "geeks for" Output: file2.txt : "geeks for geeks"
方法: 1) 打开 文件txt 输入流和 文件2。txt 在outputstream中使用append选项,这样文件的前一个内容就不会被删除。 2) 检查打开或定位文件时是否有错误。如果是,则抛出错误消息。 3) 如果两个文件都找到了,则将内容从源文件写入目标文件。 4) 显示目标文件的内容。
// C++ implementation to append // content from source file to // destination file #include <bits/stdc++.h> using namespace std; // driver code int main() { fstream file; // Input stream class to // operate on files. ifstream ifile( "file.txt" , ios::in); // Output stream class to // operate on files. ofstream ofile( "file2.txt" , ios::out | ios::app); // check if file exists if (!ifile.is_open()) { // file not found (i.e, not opened). // Print an error message. cout << "file not found" ; } else { // then add more lines to // the file if need be ofile << ifile.rdbuf(); } string word; // opening file file.open( "file2.txt" ); // extracting words form the file while (file >> word) { // displaying content of // destination file cout << word << " " ; } return 0; } |
输出:
geeks for geeks
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END