实验6 流类库与I/O

实验目的


 

1. 理解流的概念,掌握流类库的基础知识

2. 掌握标准I/O及常用的格式控制

3. 掌握文件I/O的方法

实验准备


 

1. 概念
流 插入运算符<<在使用时,向流中插入意指什么?

提取运算符>>在使用时,从流中提取意指什么?

几个标准I/O流对象:cin, cout, cerr, clog

标准I/O流类的继承/派生层次关系

2. 语法和应用

格式控制方法

使用ios类的成员函数进行格式控制
使用操纵符函数进行格式控制

使用文件流类实现文件I/O的步骤和方法,以及常用的一些成员函数或普通函数

如××.open(), ××.close(), ××.fail(), ××.is_open(),××.get(), ××.getline(), getline()等等

实验结论


 

part 2

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
    char a[20]="merge successfully.";
    ofstream ou;
    ou.open("3.txt",ios_base::app);
    ou<<"merge successfully."<<endl;
    ou.close();
system("pause");
return 0;
}
View Code

截图:

part 3

//不会重复

//但没有写文件操作

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();
utils.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));
utils.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

 

截图:

 

 

part 4

 //普通文章的标点情况太多,不好判断(没做)

#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 << "选择你想进行的操作" << endl;
    cout << "1--统计字符数" << endl;
    cout << "2--统计单词数" << endl;
    cout << "3--统计行数数" << endl;
    cout << "4--三位一体" << endl;
    cout << "0--退出" << endl;
    do{
        cout << "输入想进行的操作:";
        cin >> op;
        switch (op) {
        case '1':
            cout << "字符数:" << zifu << endl;
            continue;
        case '2':
            cout << "单词数" << danci << endl;
            continue;
        case '3':
            cout << "行数" << hang << endl;
            continue;
        case '4':
            cout << "字符数:" << zifu << endl;
            cout << "单词数" << danci << endl;
            cout << "行数" << hang << endl;
            continue;
        case '0':
            cout << "退出程序" << endl;
            continue;
        default:
            cout << "错误输入,请重新输入" << endl;
            continue;
        }
    } while (op != '0');
    system("pause");
    
    return 0;
}
main.cpp

 

截图:

 

 

实验评价


 

1.一些基础的函数记得不清楚,导致随机的时候花了些时间。

2.处理文件时,我是一个个字符判断的,应该有更好的判断方法。

3.对于打开文件和流的输入输出还比较生疏。

评价


 

1.

2.

3.

posted @ 2019-06-15 22:21  无名的路人  阅读(158)  评论(1编辑  收藏  举报