实验六

时间:2019-06-12
本文章向大家介绍实验六,主要包括实验六使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.(1)合并两个文件到新文件中。文件名均从键盘输入。 运行程序,结合运行结果及源码中注释,理解和体会文件I/O的方法。

(2)使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. " 

#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;        // 输入文件流对象 
    ofstream file;

    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的关联
    file.open(newfilename, ios_base::app);
    file << endl << "merge successfully. ";
    system("pause");

    return 0;
}
main.cpp

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

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

using namespace std;

int main() {
    
    string filename,newfilename;
    int n=1,k,c;
    newfilename = getCurrentDate();
    fstream file;
    ofstream fileout(newfilename);
    cout<<"输入名单列表文件名:";
    cin>>filename;
    cout<<"输入随机抽点人数:";
    cin>>k;
    
    file.open(filename);
    char buffer[256];
    int x[100],i=0;
    srand(time(NULL));
    for(int j=0;j<k;j++){
        x[j]=rand()%(83-1+1)+1;
        i++;
    }
    i=0;
    for(int q=0;q<k-1;q++)
    {
        for(int m=0;m<k-1-q;m++)
        {
            if(x[m]>x[m+1])
            {
                c=x[m];
                x[m]=x[m+1];
                x[m+1]=c;
            }
        }
    }

    while(!file.eof())
    {
        file.getline(buffer,256,'\n');
        if(n==x[i])
        {
        cout<<buffer<<endl;
        fileout<<buffer<<endl;
        i=i+1;
        }
        n=n+1;
    }
    file.close();
    system("pause");
    
    return 0;
}
 
main.cpp
//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

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

 3.编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。 

#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    char a[100];

    string  newfilename;
    cout<<"输入要统计的英文文本文件名:";
    cin>>newfilename;
    int n = 0,s=0,p=0,count=0,i=0;

       // 输出文件流对象 
    ifstream fin;        // 输入文件流对象 
    ofstream file;

    fin.open(newfilename);  // 将输入文件流对象fin与文件filename1建立关联 
    if (!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
        cerr << "fail to open file " << newfilename<< endl;
        system("pause");
        exit(0);
    }
    char ch;

    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
    while (fin.get(ch))
    {
        if(ch!='\n')
        n = n + 1;
        if (ch == '\n')
            s = s + 1;
        if (ch != ' '&&ch != ','&&ch != '\n')
            count += 1;
        else if ((ch == ' ' || ch == ',' || ch == '\n') && count != 0)
        {
            p = p + 1;
            count = 0;
        }
    }

    fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 

    file.open(newfilename, ios_base::app);\
    if(count==0)
    file<<endl<<"字符数:"<< n<<endl<<"单词数:"<<p<< endl<<"行数:"<<s+1<<endl;
    else
    file<<endl<<"字符数:"<< n<<endl<<"单词数:"<<p+1<< endl<<"行数:"<<s+1<<endl;
    system("pause");

    return 0;
}
main.cpp

 1.我把验证性实验的第一题和第二题放在一起做了,这样做要先关闭文件输入流对象fin与文件filename1的关联,关闭文件输入流对象fin与文件filename2的关联,关闭文件输出流对象fout与文件newfilename的关联,这样代码file.open(newfilename, ios_base::app); file << endl << "merge successfully. "才有效果,否则是没有merge successfully在结尾的。

2.在第二个随机抽取实验中要先掌握行读取,然后再运用抽取随机数的方法抽五个不同随机数放入数组,然后从小到大排序。然后就按照下列算法即可输出五个随机行。

while(!file.eof())
    {
        file.getline(buffer,256,'\n');
        if(n==x[i])
        {
        cout<<buffer<<endl;
        fileout<<buffer<<endl;
        i=i+1;
        }
        n=n+1;
    }

但是由于我能力有限,无法做到随机五个打乱顺序的输出。但是这五个随机数是随机取的只是我帮它排了一下顺序问题不大。

3.计算英文文本的字符数,行数,单词数,那题我觉得我对方法可能只对本题有用,但是我已经在我范围内尽可能改了(o_ _)ノ。

原文地址:https://www.cnblogs.com/jiyuanxiangzhouziying/p/11013226.html