菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

2023.5.10

复制代码
 1 [实验任务四]:结构体数据的二进制文件写入
 2 【问题描述】
 3 定义描述的教师的结构体,依次包含如下属性,工号(int型),姓名(string型),性别(char型),用户输入整数N,描述需要输入教师信息的个数,之后依次输入教师信息,并将教师信息写入工程目录下teacher.dat文件中,以二进制的形式写入
 4 【输入形式】
 5 N值
 6 第1个教师的工号 姓名 性别
 7 第2个教师的工号 姓名 性别
 8 。。。
 9 第N个教师的工号 姓名 性别
10 【输出形式】
11 teacher.dat文件,N个教师信息,二进制形式
复制代码
复制代码
 1 //实验8任务4
 2 #include <string>
 3 #include <fstream>
 4 #include <iomanip>
 5 #include <iostream>
 6 using namespace std;
 7 class Teacher
 8 {
 9 public:
10     Teacher() {}
11     Teacher(int m_ID,string m_name,char m_c)
12     {
13         ID = m_ID;
14         name = m_name;
15         c = m_c;
16     }
17 public:
18     int ID;
19     string name;
20     char c;
21 };
22 void test01()
23 {
24     Teacher t;
25     int num;
26     cin >> num;
27     ofstream ofs;
28     ofs.open("test1.txt", ios::out | ios::binary);
29     ofs << num<<endl;
30     for (int i = 0; i < num; i++)
31     {
32         cin >> t.ID >> t.name >> t.c;
33         ofs << t.ID << t.name << t.c<<endl;
34     }
35     ofs.close();
36 }
37 int main()
38 {
39     test01(); 
40     return 0;
41 }
复制代码
复制代码
 1 //实验八任务六
 2 #include <iostream>
 3 #include <fstream>
 4 using namespace std;
 5 void test01()
 6 {
 7     ofstream ofs;
 8     ofs.open("M99.txt", ios::out);
 9     for (int i = 1; i <= 9; i++)
10     {
11         for (int j = 1; j <= i; j++)
12         {
13             ofs << j << "*" << i << " = " << i * j <<"  ";
14         }
15         ofs << endl;
16     }
17     ofs.close();
18 }
19 int main()
20 {
21     test01();
22     return 0;
23 }
复制代码

 

posted on   hhmzd233  阅读(19)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示