C++把结构体数组写入配置文件
#include<iostream>
#include<vector>
using namespace std;
typedef struct NODE
{
int x;
int y;
}NODE;
int main()
{
vector<NODE> vec;
srand(time(NULL));
for (int i = 0; i < 10; ++i)
{
NODE tep;
tep.x = i+rand()%100;
tep.y = i+rand()%200;
vec.push_back(tep);
}
FILE* fp = fopen("D:\\1.txt", "w+");
if (fp == NULL)
{
return -1;
}
for (int i = 0; i < vec.size(); ++i)
{
fwrite(&vec[i], 1, sizeof(NODE), fp);
}
fclose(fp);
fp = fopen("D:\\1.txt", "r");
if(fp==NULL)
{
return -1;
}
vector<NODE>mm;
for (int i = 0; i < 10; ++i)
{
NODE tep;
fread(&tep, 1, sizeof(NODE), fp);
mm.push_back(tep);
cout << tep.x << " " << tep.y << endl;
}
return 0;
}