实验6——流类库与I/O

  实验目的
1. 理解流的概念,掌握流类库的基础知识
2. 掌握标准I/O及常用的格式控制
3. 掌握文件I/O的方法
  实验准备
  语法和应用
  格式控制方法
使用ios类的成员函数进行格式控制使用操纵符函数进行格式控制
使用文件流类实现文件I/O的步骤和方法,以及常用的一些成员函数或普通函数,如××.open(), ××.close(),
××.fail(), ××.is_open(),××.get(), ××.getline(), getline()等等。
Part1 验证性实验
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;
}

main.cpp
part1

 

 

 

Part2 基础练习
使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "
 
 
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main() {
    string filename, str;

    cout << "请输入要追加内容的文件名:";
    cin >> filename;
    cout << endl << "请输入要追加的内容:";
    getline(cin, str,'o');

    ofstream out(filename,ios_base::app);//打开一个输出文件用于在文件尾部添加数据
    if (out.is_open()) {
        out << str;
        out.close();
    }
    system("pause");
    return 0;
}
part2

Part3 应用编程实践
1. 已知名单列表文件list.txt。编写一个应用程序,实现从名单中随机抽点n位同学(n由键盘输入),在屏幕上显
示结果,同时也将结果写入文本文件,文件名自动读取当天系统日期,如20190611.txt。
 
 
#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();
choose.h
#include "utils.h"
#include <ctime>
using std::string;

const int SIZE = 20;

// 函数功能描述:返回当前系统时间
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611
string getCurrentDate() {
    
    time_t time_seconds = time(0);
    struct tm now_time;
    localtime_s(&now_time, &time_seconds); // 使用了更安全的localtime_s()
    
    char date[SIZE];

    strftime(date, SIZE, "%Y%m%d", &now_time);
    
    return (string(date));
choose.cpp
#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include<ctime>
#include "utils.h"

using namespace std;

void suiji(int x,int n,int jud[]){
    int a = n,b;
    srand(int(time(NULL)));
    for (n; n > 0; n--) {
        jud[n - 1] = rand() % x;
        for (b = a; b > n; b--) {
            if (jud[b - 1] == jud[n - 1]) {
                n++;
                break;
            }
        }
    }
}

int main() {
    string number[1000];
    int jud[1000];
    string name1;
    int a=0,b;
    int max=0;
    int n;


    cout<<"输入名单列表文件名:";
    cin>>name1;
    ifstream file;


    file.open(name1);
    while(getline(file,number[a++])){
        max++;
    }

    cout<<"输入随机抽点人数:";
    cin>>n;
    cout<<"随机抽点中..."<<endl;



    string filename;
    
    filename = getCurrentDate();
    

    ofstream fileo;
    fileo.open(filename);
    suiji(max,n,jud);
    for(n;n>0;n--){
        cout << number[jud[n-1]] << endl;
        fileo<< number[jud[n-1]] << endl;
    }

    system("pause");
    
    return 0;
}
main.cpp

#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>

using namespace std;


int main() {
    string str;
    cout << "输入要统计的英文文本名";
    cin >> str;
    char op;
    ifstream file;
    file.open(str);
    int zifu=0,danci=1,hang=1;
    string a;
    char ch;
    while(file.get(ch)){
        if (ch =='\n') {
         hang++;
        danci++;
        }
        else {
            zifu++;
            if (ch == ' ')
                danci++;
        }
    }
            cout << "字符数:" << zifu << endl;
            cout << "单词数:" << danci << endl;
            cout << "行数:" << hang << endl;
   

2. 编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。
#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>

using namespace std;


int main() {
    string str;
    cout << "输入要统计的英文文本名";
    cin >> str;
    char op;
    ifstream file;
    file.open(str);
    int zifu=0,danci=1,hang=1;
    string a;
    char ch;
    while(file.get(ch)){
        if (ch =='\n') {
         hang++;
        danci++;
        }
        else {
            zifu++;
            if (ch == ' ')
                danci++;
        }
    }
            cout << "字符数:" << zifu << endl;
            cout << "单词数:" << danci << endl;
            cout << "行数:" << hang << endl;
    return 0;
}
check

总结:1.学会使用getline进行读取特定字符后终止的操作。

   2.随机抽人借助rand()和srand()进行假随机和真随机操作。

 

posted @ 2019-06-19 09:48  卢涉  阅读(176)  评论(0编辑  收藏  举报