#include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; int main() { string filename1, filename2, newfilename; cout << "输入要合并的两个文件名: " ; cin >> filename1 >> filename2; cout << "输入合并后新文件名: " ; cin >> newfilename; ofstream fout; // 输出文件流对象 ifstream fin; // 输入文件流对象 fin.open(filename1); // 将输入文件流对象fin与文件filename1建立关联 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出 cerr << "fail to open file " << filename1 << endl; exit(0); } fout.open(newfilename); // 将输出文件流对象fout与文件newfilename建立关联 if(!fin.is_open()) { // 如果创建/打开文件失败,输出错误提示信息并退出 cerr << "fail to open file " << newfilename << endl; exit(0); } char ch; // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 while(fin.get(ch)) fout << ch; fin.close(); // 关闭文件输入流对象fin与文件filename1的关联 fout << endl; // 向文件输出流对象fout中插入换行 fin.open(filename2); // 将输入文件流对象fin与文件filename2建立关联 if(!fin.is_open()) { // 如果打开文件失败,则输出错误提示信息并退出 cerr << "fail to open file " << filename2 << endl; exit(0); } // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 while(fin.get(ch)) fout << ch; fin.close(); // 关闭文件输入流对象fin与文件filename2的关联 fout.close(); // 关闭文件输出流对象fout与文件newfilename的关联 ofstream fapp; fapp.open(newfilename,ios_base::app); fapp<<endl; fapp<<"merge successfully."; fapp.close(); system("pause"); return 0; }
#include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; int main(){ string txtname,line0; char x[1000]; cout<<"输入要统计的英文文本文件名:"; cin>>txtname; ifstream fin; int n=0,i=1,j=1; char c; fin.open(txtname); if(!fin.is_open()){ cerr<<"fail to open file "<<txtname<<endl; exit(0); } while(fin.get(c)){ if(c!=‘\n‘){ n++; if(c==‘ ‘) j++; } else {j++; i++; } } cout<<"字符数:"<<n<<endl; cout<<"单词数:"<<j<<endl; cout<<"行数:"<<i<<endl; return 0; }
总结:还是不能熟练地完成编程,第二题实在没有做出来。
原文:https://www.cnblogs.com/jessi-wu1005/p/11042456.html