- #include <fstream>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- using namespace std;
- struct Employee
- {
- std::string name;
- std::string department;
- double salary;
- int age;
- bool gender;
- };
- int main()
- {
- FILE *stream;
- char filename[16] = "a.txt";
- stream = fopen(filename, "a+");//以追加的方式打开
- vector<Employee> empls;
- Employee object ={ "张三", "man", 320, 34, 1 };
- for (int i=1; i<=5; i++)
- empls.push_back(object);
- //这个fprintf只支持C风格的符串,所以it->name应该改为it->name.c_str()
- for(vector<Employee>::iterator it=empls.begin();it!=empls.end();++it)
- {
- fprintf(stream, "%s %s %f %d %d\n", it->name.c_str(), it->department.c_str(),
- it->salary, it->age, it->gender);
- }
- return 0;
- }
fprintf写文件
最新推荐文章于 2023-06-03 14:19:32 发布