vector的文件读写
vector<COORD>g_VecSnake;保存了蛇的坐标,怎么 fwrite 进文件啊 #include <vector>
#include <fstream>
#include <iostream.h>
typedef struct tagCoordinate
{
int x;
int y;
tagCoordinate(int _x,int _y)
{
x = _x;
y = _y;
}
}COOR, *LPCOOR;
using namespace std;
int main(void)
{
vector<COOR> vcr;
for(int i = 0 ; i < 10 ; i ++)
{
vcr.push_back(COOR(i * 2 + 1, i * 3 + 2));
}
ofstream fWrite("e:\\snake.dat",ios::out);
typedef std::vector<COOR>::iterator VI;
for(VI vi = vcr.begin() ; vi != vcr.end() ; vi++)
{
fWrite << vi->x << ' ' << vi->y << ' ';
}
fWrite.close();
ifstream fRead("e:\\snake.dat",ios::in);
COOR cr(0,0);
while(!fRead.eof())
{
fRead >> cr.x >> cr.y;
cout << cr.x << ',' << cr.y << endl;
}
return 0;
}
页:
[1]