实验六

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

1.

使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. " :
代码:
 1 // 合并两个文件内容到一个新文件中。
 2 // 文件名均从键盘输入
 3 
 4 #include <iostream>
 5 #include <fstream>   
 6 #include <string>
 7 #include <cstdlib>
 8 #include<vector>
 9 using namespace std;
10 
11 int main() {
12     string filename1, filename2, newfilename;
13 
14     //cout << "输入要合并的两个文件名: ";
15     //cin >> filename1 >> filename2;
16     //cout << "输入合并后新文件名: ";
17     //cin >> newfilename;
18 
19     ofstream fout;        // 输出文件流对象 
20     ifstream fin;        // 输入文件流对象 
21 
22 
23     fin.open("1.txt");  // 将输入文件流对象fin与文件filename1建立关联 
24     if (!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
25         cerr << "fail to open file " << filename1 << endl;
26         system("pause");
27         exit(0);
28     }
29 
30     fout.open("3.txt");    // 将输出文件流对象fout与文件newfilename建立关联 
31     if (!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
32         cerr << "fail to open file " << newfilename << endl;
33         system("pause");
34         exit(0);
35     }
36 
37     char ch;
38 
39     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
40     while (fin.get(ch))
41         fout << ch;
42 
43     fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
44 
45     fout << endl; // 向文件输出流对象fout中插入换行 
46 
47 
48     fin.open("2.txt");  // 将输入文件流对象fin与文件filename2建立关联 
49     if (!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
50         cerr << "fail to open file " << filename2 << endl;
51         system("pause");
52         exit(0);
53     }
54 
55     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
56     while (fin.get(ch))
57         fout << ch;
58     fout << endl;
59     fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
60     fout.close();
61     ofstream write;
62     write.open("3.txt", ios::app);
63     string a = "merge sucessfully.";
64     write << a << endl;
65     write.close();
66     // 关闭文件输出流对象fout与文件newfilename的关联
67 
68     system("pause");
69 
70     return 0;
71 }

截图:

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

结构体写法:

#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
#include<time.h>
using namespace std;
struct student {
    string order;
    string xuehao;
    string name;
    string classname;
};
int main() {
    int n;
    student s[83];
    ifstream filea("listr.txt");
    if (!filea)                 //测试是否成功打开  
    {
        cerr << "open error!" << endl;
        return 0;
    }
    int i = 0;
    while (filea >> s[i].order >> s[i].xuehao >> s[i].name >> s[i].classname)
    {
        i++;
    }
    filea.close();
    int a = 1;
    time_t nowtime = time(NULL);
    struct tm *p;
    p = gmtime(&nowtime);
 
    char filename[256] = {0};
    sprintf(filename,"%d-%d-%d.txt",1900+p->tm_year,1+p->tm_mon,p->tm_mday);
    ofstream out(filename);
    cout << "Please input the number: " << endl;
    cin >> n;
    int k;
    k = n;
    while (k--)
    {
        a = rand() % n + 1;//产生一个1到i的随机数; 
        cout << s[a].order << " " << s[a].xuehao << " " << s[a].name << " " << s[a].classname << endl;
        out << s[a].order << " " << s[a].xuehao << " " << s[a].name << " " << s[a].classname << endl;
    }
    out.close();

    return 0;
}

本来觉得结构体的方法更为简便,但想起了期中考试里的动态数组,觉得此题类似也可以定义一个类运用动态数组来写:

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<vector>
using namespace std;
class student{
    public:
    student(string o,string x,string n,string cn):order(o),xuehao(x),name(n),classname(cn){}
    void print();
    string geto();
    string getx();
    string getn();
    string getcn();
    private:
        string order;
      string xuehao;
        string name;
        string classname;        
};
void student::print(){
     cout<<order<<" "<<xuehao<<" "<<name<<" "<<classname<<endl;
}
string student::getcn(){
    return classname;
}
string student::geto(){
    return order;
}
string student::getn(){
    return name;
}
string student::getx(){
    return xuehao;
}
int main(){
    vector<student>s;
    string order,xuehao,name,classname;
    int n;
    int i=0;
    ifstream in("listr.txt");
    if(!in)
    {
        cout<<"OPEN ERROR!"<<endl;
        return 0;
    }
  while(in>>order>>xuehao>>name>>classname)
  {
      student st(order,xuehao,name,classname);
      i++;
      s.push_back(st);
  }
  in.close();
  ofstream out("roll.txt"); 
  cout<<"PLEASE INPUT: "<<endl;
  cin>>n;
 int  k=n;
  while(k--)
  {
 int     a=rand()%n+1;
      s[a].print();
      out<<s[a].geto()<<" "<<s[a].getx()<<" "<<s[a].getn()<<" "<<s[a].getcn()<<endl;
  }
out.close();    
return 0;
}

截图:

2. 编程统计英文文本文件中字符数(包括空格)、单词数、行数。文件名由键盘输入。
必做
用c++编程实现题目的基本功能要求:
代码:
#include<string.h>
#include<fstream>
#include<iostream>
using namespace std;
int main(){
ifstream in("article");
    long linenum=0,chnum=0,wordnum=0;
    char str[1000];
    while(in.getline(str,1000))
    {
        for(int i=0;i<strlen(str);i++)
        {
            chnum++;
            if(str[i]==' '||str[i]=='...'||(str[i]=='.'&&str[i+1]!='.'&&str[i-1]!='.')||str[i]==',')
            {
                wordnum++;
            }
        }
        linenum++;
    }
   
    cout<<"行数:"<<linenum<<endl<<"字符数:"<<chnum<<endl<<"单词数:"<<wordnum<<endl; 
    in.close();
    return 0;
}

图片:

 实验结论:
本次实验是我有一次复习了结构体以及动态数组等问题,发现对待问题不应一概而论,应抓住重点,选择最优方法。

在实验的过程中我也发现了相关问题,就是打开文件时,文件名有时是ifstream“xxx.txt”可以打开,有时只能是“xxx”才能打开,到底是为什么?

原文地址:https://www.cnblogs.com/lszz/p/10993591.html