#include <iostream> #include <iomanip> #include <math.h> #include <fstream> using namespace std; #include<string.h> class Student { public: Student(){} Student(char *no,char *name,char *a) { strcpy(sno,no);//将局部变量复制给数据变量 strcpy(sname,name); strcpy(age,a); } char sno[10], sname[20],age[3]; }; void filewrite() { ofstream os; os.open("C:\\Users\25604\Documents\file\data.txt",ios::out|ios::binary); Student s = {"1","rose","20"}; os.write((const char *)&s,sizeof(s));//将对象强转成指针变量 os.close(); } void fileread()//从文件中读取数据 { ifstream it;//创建读取流对象(输入文件流对象) it.open("C:\\Users\25604\Documents\file\data.txt",ios::in|ios::binary);//打开文件 if(!it.is_open()) { cout<<"打开文件失败\n"; } Student s; it.read((char *)&s,sizeof(s));//读取记录 cout<<"学号:"<<s.sno<<"姓名:"<<s.sname<<"年龄:"<<s.age<<endl; } int main() { filewrite();//写入函数,写入数据 fileread(); return 0; }
时间: 2024-04-25 16:27:00 浏览: 22
这段代码是一个简单的文件读写程序,通过在文件中存储和读取学生信息来实现。
代码中首先定义了一个 `Student` 类,包含学生的学号、姓名和年龄。然后定义了两个函数,`filewrite()` 和 `fileread()`,分别用于向文件写入数据和从文件中读取数据。在 `filewrite()` 函数中,先创建一个 `Student` 对象,并将其成员变量赋值为指定的值,然后打开文件并将这个对象写入到文件中。在 `fileread()` 函数中,先打开文件,然后从文件中读取一个 `Student` 对象,并输出其成员变量的值。
在 `main()` 函数中,先调用 `filewrite()` 函数将数据写入文件,然后调用 `fileread()` 函数读取文件中的数据并输出。
相关问题
#include <string.h>#include <iomanip>
这是C++中引入头文件的方式,`#include <string.h>`用于字符串处理,`#include <iomanip>`则用于流输入输出流的格式控制。下面是两个头文件的简介:
1.`#include <string.h>`是C和C++的标准头文件之一,提供了字符串处理函数和一些有关内存操作的函数。
2.`#include <iomanip>`则提供了一组流操纵符和一些操纵符的操作函数,可以用于格式控制输出流的各种格式。
举个例子,下面的代码演示了如何使用`<iomanip>`头文件中的操作符`setw()`设置输出的字段宽度为10个字符:
```c++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x = 123456;
cout << "x = " << setw(10) << x << endl;
return 0;
}
```
运行结果为:
```
x = 123456
```
统一文件名只用#include<iostream> #include<string.h>
这两个头文件的作用不同,<iostream> 是用于输入输出流的库,而<string.h> 是用于字符串处理的库。如果你的程序中需要使用字符串相关函数,如strcpy、strlen等,就需要用到<string.h>头文件。如果要使用cout和cin等输入输出流对象则需要使用<iostream>头文件。如果你两个都需要用到,那么你就需要同时包含这两个头文件。例如:
```cpp
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str1[10] = "hello";
char str2[10] = "world";
strcat(str1, str2); // 使用<string.h>中的字符串拼接函数strcat
cout << str1 << endl; // 使用<iostream>中的输出流对象cout
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)