实验六

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

// 合并两个文件内容到一个新文件中。
// 文件名均从键盘输入
#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.使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. "

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

using namespace std;

int main()
{ofstream file;
 string a;

 file.open("3.txt",ios_base::app);
 file<<"merge successfully. "<<endl;
 file.close();

 return 0;
}
View Code

 

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

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

using namespace std;

int main() {
    int n,line=0,t;
    string filename,listfile,a[100],b;
    ifstream file1;
    ofstream file2;
        
    filename = getCurrentDate();

    cout << "输入名单列表文件名: " ;
    cin >> listfile;
    cout << "输入随机抽点人数:";
    cin >> n;

    cout << filename << endl;

    file1.open(listfile);// 将输入文件流对象file1与文件listname建立关联
    if (!file1.is_open())
    {
        cerr << "fail to open file " <<listfile<< endl;
        system("pause");
        exit(0);
    }
    file2.open(filename);
    if (!file2.is_open())
    {
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);
    }
    
    cout << "随机抽点中......" << endl;

    srand(time(NULL));    
    while (getline(file1,b))
    {
        line++;
        a[line] = b;
    }
    while (n--)
    {
        t = rand() % 83 + 1;
        file2 << a[t] << endl;
        cout << a[t] << endl;
    }

    file1.close();
    file2.close();

    system("pause");
    
    return 0;
}
 
main.cpp
//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190607
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

 

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

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

using namespace std;

int main()
{
    string filename;
    cout << "输入要统计的英文文本文件名:";
    cin >> filename;

    ifstream fin;
    fin.open(filename);
    if (!fin.is_open())
    {cerr << "fail to open file " << filename << endl; 
     system("pause");        
     exit(0);        
    } 

    char ch;
    string b,a[100];
    int chars=0, words=0, lines=0,flag1=1,flag2=1;
    while (fin.get(ch))
    {
        chars++;

        if (ch >= 'a'&&ch <= 'z')
            flag2 = 1;
        else
            flag2 = 0;
        if (flag1 == 1 && flag2 == 0)
        {
            words++;
        }
        flag1 = flag2;    
    
        if (ch == '\n')
            lines++;
    }

    cout << "字符数:" << chars -lines<< endl//去掉\n
        << "单词数:" << words << endl
        << "行数:" << lines+1 << endl;//最后一行没有\n

    fin.close();

    system("pause");

    return 0;
}
View Code

 

总结:一开始做三题的时候,思路挺混乱的,不知道怎么在文件中随机读取某一行。后来参考同学的发现是把整个文件按行存在字符串数组中,随机出数组下标。

第四题思路上没什么问题,就是本来行数打算用getline但答案一直是0,后来用了另一种方法

 

 

互评:

https://www.cnblogs.com/nnn13579/

https://www.cnblogs.com/knight04/

https://www.cnblogs.com/0122Frank/

posted @ 2019-06-17 15:47  shona小暖  阅读(131)  评论(2编辑  收藏  举报