1.合并两个文件

#include <iostream>
#include <fstream>   
#include <string>
#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;
        system("pause");
        exit(0);    
    }
    
    fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
    if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        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;
        system("pause");
        exit(0);    
    }
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
    while(fin.get(ch))
        fout << ch;
    
    fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
} 
View Code

       

 

2.追加

#include <iostream>
#include <fstream>   
#include <string>
#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;
        system("pause");
        exit(0);    
    }
    
    fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
    if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        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;
        system("pause");
        exit(0);    
    }
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
    while(fin.get(ch))
        fout << ch;
    
    fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联
    
    ofstream fapp;
    fapp.open(newfilename,ios::app);
    if(!fapp.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
        cerr << "fail to open file "  << endl;
        system("pause");
        exit(0);    
    }
    fapp<<endl<<"merge successfully.";
    fapp.close();

    system("pause");
    
    return 0;
} 

3.点名

#include <iostream>
#include <string>
#include <cstdlib>
#include<fstream>
#include<time.h>
#include "utils.h"

using namespace std;

int main() {
    
    string filename;
    int n, t = 0, i;
    
    ofstream fout;
    ifstream fin;
    
    cout << "输入名单列表文件名:";
    cin >> filename;
    cout << "输入随机抽点人数:";
    cin >> n;
    fin.open(filename, ios_base::in);
    if (!fin.is_open()) {
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);
    }
    string s, a[100];
    if (fin) {
        while (getline(fin, s)) {
            a[t++] = s;
        }
        fin.close();
    }
    string filename1;
    filename = getCurrentDate();
    cout << "随机抽点中..." << filename1<< endl;
    srand(time(0));
    for (i=1;i<=n;i++) {
        int num;
        int r = rand();
        num= rand() % t;
        cout <<  a[num] << endl;
        fout.open(filename, ios_base::app);
        fout << a[num] << endl;
        fout.close();
    }

    system("pause");
    
    return 0;
}
 
View Code

4. 统计

#include <iostream>
#include <fstream>   
#include <string>
using namespace std;
int main() {
    string filename;      
    cout<<"输入要统计的文本文件名:" ;
    cin>>filename;
    ifstream fin;
    ofstream out;
    fin.open(filename);
    if(!fin.is_open()) {
        cerr << "fail to open file "<<endl;
        system("pause");
        exit(0);    
    }
    char ch;
    int s=0,word=0,line=1;
    while(fin.get(ch)){
        if(ch==' '){
            word++;
            s++;
        }
        else if(ch!=' '&&ch!='\n'){
            s++;
        }
        else{
            line++;
        }
    }
        cout<<"字符数:"<<s<<endl;
        cout<<"单词数:"<<word+1<<endl;
        cout<<"行数:"<<line<<endl;         
        fin.close();
 } 
View Code

总结:

这次的实验在一开始根本不知道该去怎么做,甚至一开始连第一题的验证都做不出来,最后在查了好多资料后才终于开始写程序,但在统计单词数里面不知道哪里出现了问题,就是少了两个

评论:

1.https://www.cnblogs.com/charlotte00/p/11031967.html

2.https://www.cnblogs.com/wjh1022/p/11028328.html

posted on 2019-06-16 16:34  domestic徐婷楠  阅读(114)  评论(0编辑  收藏  举报