实验六

本文探讨了使用C++进行文件读写、学生信息抽取及文件内容统计的三种方法。涉及ofstream与ifstream的使用,包括文件打开、写入、读取及关闭等基本操作。通过实例展示了如何统计文件中的字符数、单词数和行数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int main()
 8 {
 9     string filename, t; cout << "输入文件名:" ;
10     getline(cin, filename);
11     cout << "输入要加入的内容:" ;
12     getline(cin, t);
13     
14     ofstream a(filename, ios::app);
15     if (!a)
16     {
17         cout << "wrong" << endl;
18     }
19     if (a)
20     {
21         a << t << endl;
22     }
23     system("pause");
24     return 0;
25 }

 

二、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 #include"utils.h"
 7 using namespace std;
 8 struct Student
 9 {
10     string mynumber;
11     string xuehao;
12     string myname;
13     string myclass;
14 };
15 int main()
16 {
17     string t;
18     cout << "输入文件名:";
19     cin >> t;
20     ifstream file1(t, ios_base::in);
21     if (!file1)
22     {
23         cout << "wrong" << endl;
24     }
25     int i;
26     Student a[83];
27     for (i = 0; i < 83; i++)
28     {
29         file1 >> a[i].mynumber >> a[i].xuehao >> a[i].myname >> a[i].myclass;
30     }
31     string b;int x = -1;
32     b = getCurrentDate() + ".txt";
33     ofstream file2(b, ios::out);
34     cout << "抽取人数:";
35     int m; 
36     cin >> m;
37     if (!file2)
38     {
39         cout << "wrong" << endl;
40     }
41     srand(time(0)); 
42     for (i = 0; i < m; i++) {
43         int x = rand() % (83) - 1;
44             cout << a[x].mynumber << " " << a[x].xuehao << " " << a[x].myname << " " << a[x].myclass << endl;
45             file2 << a[x].mynumber << " " << a[x].xuehao << " " << a[x].myname << " " << a[x].myclass << endl;
46         }
47         
48     file1.close();
49     file2.close();
50     system("pause");
51     return 0;
52 
53 
54 
55 
56 
57 }

 

 

 

三、

 1 #include <iostream>
 2 #include <string>
 3 #include<ctime>
 4 #include<fstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 int main()
 8 {
 9     string name,a;
10     string myarray[1000]; int m=0,i=0,count=0,sum=0;
11     cout << "输入文件名:";
12     cin >> name;
13     ifstream file1(name, ios_base::in);
14     if (!file1)
15     {
16         cout << "wrong" << endl;
17     }
18     if (file1) {
19         while (getline(file1, a))
20         {
21             myarray[m++] = a;
22             for (i = 0; i < a.length(); i++)
23             {    
24                 if (a[i] == ' '&&a[i + 1] != ' ')
25                 {
26                     count++;
27                 }
28                 
29             }
30             count++;
31         }
32     }
33         file1.close();
34     for (i = 0; i < m; i++)
35     {
36         sum += myarray[i].length();
37     }
38     cout << "字符数:" << sum << endl;
39     cout << "单词数:" << count << endl;
40     cout << "行数:" << m << endl;
41     system("pause");
42     return 0;
43 
44 }

 

总结:

这次作业总体来说不是特别难,但因为有些地方一开始不太懂,导致开始写的时候进度特别缓慢。

1、ofstream a(filename,ios::out)和ofstream a;a.open(filename)好像区别不是很大。

2、抽取人数那里的 重复问题暂时还没解决。

3、数单词的题目似乎我把它想复杂了,其实只是多一个ifstream的步骤。

 

转载于:https://www.cnblogs.com/0122Frank/p/11019463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值