实验六---流类库与I/O

 1 // 合并两个文件内容到一个新文件中。
 2 // 文件名均从键盘输入
 3 
 4 #include <iostream>
 5 #include <fstream>   
 6 #include <string>
 7 #include <cstdlib>
 8 using namespace std;
 9 
10 int main() {
11     string filename1, filename2, newfilename;
12     
13     cout << "输入要合并的两个文件名: " ;
14     cin >> filename1 >> filename2;
15     cout << "输入合并后新文件名: " ;
16     cin >> newfilename;
17     
18     ofstream fout;        // 输出文件流对象 
19     ifstream fin;        // 输入文件流对象 
20     
21     
22     fin.open(filename1);  // 将输入文件流对象fin与文件filename1建立关联 
23     if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出 
24         cerr << "fail to open file " << filename1 << endl;
25         system("pause");
26         exit(0);    
27     }
28     
29     fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
30     if(!fin.is_open()) {  // 如果创建/打开文件失败,输出错误提示信息并退出  
31         cerr << "fail to open file " << newfilename << endl;
32         system("pause");
33         exit(0);
34     }
35     
36     char ch;
37     
38     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
39     while(fin.get(ch)) 
40         fout << ch;
41     
42     fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
43     
44     fout << endl; // 向文件输出流对象fout中插入换行 
45     
46     
47     fin.open(filename2);  // 将输入文件流对象fin与文件filename2建立关联 
48     if(!fin.is_open()) {  // 如果打开文件失败,则输出错误提示信息并退出
49         cerr << "fail to open file " << filename2 << endl;
50         system("pause");
51         exit(0);    
52     }
53     
54     // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
55     while(fin.get(ch))
56         fout << ch;
57     
58     fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
59     fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联
60 
61     system("pause");
62     
63     return 0;
64 } 
65 
66 // 说明:
67 // 这个简单示例中,合并两个文件的具体方法,是逐个读取文件中的字符直到文件末尾,并写入到新文件中
68 // 还可以一次读取一行
69 // 或者,直接利用标准模板库的成员函数,一次性将整个文件内容读取至缓冲区
70 // 等过了期末考,时间宽松一些的时候可以学习体验更多标准模板库的内容,对后续专业课的学习(如数据结构、算法、操作系统等)也会有帮助 
ex1

2.基础练习:

 1 #include<iostream>
 2 #include<fstream>
 3 using namespace std;
 4 int main(){
 5     ofstream out("3.txt",ios_base::app);
 6     out<<"merge successfuly.";
 7     if(out==0){
 8     cout<<"ERROR:Cannot open txt."<<endl;
 9     }
10     system("pause");
11     out.close();
12 }
ex2

实验结果:

3.应用编程实践:

 1 #include <iostream>
 2 #include<fstream>
 3 #include <string>
 4 #include <cstdlib>
 5 #include<ctime>
 6 #include "utils.h"
 7 
 8 using namespace std;
 9 
10 int main() {
11     string filename;
12     filename = getCurrentDate();
13     cout<<filename<<endl;
14 
15     string name;
16     cout<<"输入名单列表文件名:";
17     cin>>name;
18     
19     ifstream fin;
20     ofstream fout;
21 
22     fin.open(name);
23 
24     if(!fin.is_open()){
25         cout<<"打开文件失败"<<endl;
26         system("pause");
27         exit(0);
28     }
29 
30     fout.open(filename);
31     if(!fout.is_open())       //判断是否打开成功 
32     {
33         cout<<"fail to open file "<<filename<<endl;
34         system("pause");
35         exit(0);
36     }
37     cout <<"打开文件名"<< filename << endl;
38     
39     cout<<"输入随机抽点人数:";
40     int n,j;
41     cin>>n;
42     cout<<"随机抽点中···"<<endl;
43     string a[85];
44     int id;
45     string s;
46     srand((unsigned)time(0));
47     for(int i=0;i<83;i++)
48     {
49         getline(fin,s);
50          a[i]=s;
51     }
52     for(int j=0;j<n;j++){
53         int l;
54         
55         l=1+rand()%83;
56         cout<<a[l]<<endl;
57         fout<<a[l]<<endl;
58     }
59     fin.close();
60     fout.close();
61     system("pause");
62     return 0;
63 }
list.mian
 1 #include "utils.h"
 2 #include <ctime>
 3 using std::string;
 4 
 5 const int SIZE = 20;
 6 
 7 // 函数功能描述:返回当前系统时间
 8 // 参数描述:无参数
 9 // 返回值描述:以string类型返回系统当前日期,格式诸如20190611
10 string getCurrentDate() {
11     
12     time_t now = time(0); // 获取当前系统日历时间
13 
14     struct tm*local_time = localtime(&now); // 把系统日历时间转换为当地时间
15     
16     char date[SIZE];
17 
18     strftime(date, SIZE, "%Y%m%d", local_time);
19     
20     return (string(date));
21 } 
list.utils.cpp
1 //这个头文件里包含了可用工具函数的声明 
2 
3 #include <string>
4 using std::string;
5 
6 // 函数声明
7 // 返回当前系统时间,格式诸如20190607
8 string getCurrentDate();  
9  
list.utils.h

运行结果:

4.应用编程实验(2):

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdlib>
 6 using namespace std;
 7 int main(){
 8     string name;
 9     ifstream fin;
10     ofstream fout;
11     cout<<"输入要打开的英文文本名:";
12     cin>>name; 
13     fin.open(name);
14     if(!fin.is_open()){
15         cout<<"打开文件失败"<<endl;
16         exit(0);
17     }
18     string s;
19     int x=0,y=0,z=0;
20     while(getline(fin,s)){
21         z++;
22         int len;
23         len=s.size();
24         x+=len;
25         char a[len];
26         for(int i=0;i<len;i++){
27             a[i]=s[i];
28         }
29         a[len-1]='\0';
30         for(int j=0;j<len;j++){
31             if(a[j]==' '&&a[j+1]!=' '||(a[j]=='.'||a[j+1]==',')&&a[j+1]!=a[j]) y++;
32         }
33     }
34     cout<<"字符数:"<<x<<endl;
35     cout<<"单词数:"<<y<<endl; 
36     cout<<"行数:"<<z<<endl;
37     fin.close();
38     system("pause");
39     return 0;
40 } 
article

运行结果:

实验结论:

文件类的这些东西之前c语言是由有接触过的,但都是很皮毛的,也基本没什么映像了,c++学的这块其实还有很多是没有用到的。写最后那个程序的时候其实也可以不用书上的getline来读取,getline是有局限性的,只能存在string 里面,从网上查了一下,可以用fin.getline(数组名,容量),前面的fin是你自己用ifstream定义的文件输入流对象;

验证性实验一里用了get,get的作用也是读入数据,有点不同的就是get读入数据包括了空白字符,get类似于提取运算符“>>”,区别是上述一点。

写程序时有踩过一个坑是判断文件是否打开的,它的格式是fin.is_open(),刚开始没有用这个,有运行错误提示,以后要好好记住不要再踩坑了。

猜你喜欢

转载自www.cnblogs.com/mxueyyqx/p/11031681.html