1 //#include <iostream>
2 //using namespace std;
3 //#include <fstream>
4 ////写文本文件
5 //void test01()
6 //{
7 // //1.包含头文件 fstream
8 // //2.创建流对象
9 // ofstream ofs;
10 // //3.指定打开方式
11 // ofs.open("test.txt",ios::out);
12 // //4.写内容
13 // ofs<<"姓名:张三"<<endl;
14 // ofs<<"性别:男"<<endl;
15 // ofs<<"年龄:18"<<endl;
16 // //5.关闭文件
17 // ofs.close();
18 //}
19 //int main()
20 //{
21 // test01();
22 // return 0;
23 //}
24 ////////////////////////////////////////////////////
25 //#include <iostream>
26 //#include <fstream>
27 //#include <string>
28 //using namespace std;
29 ////读文件
30 //void test02()
31 //{
32 // //1.包含头文件
33 // //2.创建对象
34 // ifstream ifs;
35 // //3.打开文件,并判断是否打开成功
36 // ifs.open("test.txt",ios::in);
37 // if(!ifs.is_open())
38 // {
39 // cout<<"文件打开失败"<<endl;
40 // return;
41 // }
42 // //4.读入文件
43 // string buf;
44 // while(getline(ifs,buf))
45 // {
46 // cout<<buf<<endl;
47 // }
48 // //5.关闭文件
49 // ifs.close();
50 //}
51 //int main()
52 //{
53 // test02();
54 // return 0;
55 //}
56 ////////////////////////////////////////////////////
57 //#include <iostream>
58 //#include <fstream>
59 //using namespace std;
60 ////写二进制文件
61 //class Person
62 //{
63 //public:
64 // char m_Name[64];//尽量不要用string
65 // int m_Age;
66 //};
67 //void test03()
68 //{
69 // ofstream ofs;
70 // ofs.open("person.txt",ios::out|ios::binary);
71 // Person p = {"张三",18};
72 // ofs.write((const char*)&p,sizeof(Person));
73 // ofs.close();
74 //}
75 //int main()
76 //{
77 // test03();
78 // return 0;
79 //}
80 /////////////////////////////////////////////////////
81 //#include <iostream>
82 //#include <fstream>
83 //using namespace std;
84 ////读取二进制文件
85 //class Person
86 //{
87 //public:
88 // char m_Name[64];//尽量不要用string
89 // int m_Age;
90 //};
91 //void test04()
92 //{
93 // ifstream ifs;
94 // ifs.open("person.txt",ios::in|ios::binary);
95 // if(!ifs.is_open())
96 // {
97 // cout<<"文件打开失败"<<endl;
98 // }
99 // Person p;
100 // ifs.read((char *)&p,sizeof(Person));
101 // cout<<"姓名:"<<p.m_Name<<"年龄:"<<p.m_Age<<endl;
102 // ifs.close();
103 //}
104 //int main()
105 //{
106 // test04();
107 // return 0;
108 //}