鱼C论坛

 找回密码
 立即注册
查看: 1290|回复: 1

[已解决]vector的文件读写

[复制链接]
发表于 2020-10-21 16:25:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
vector<COORD>g_VecSnake;

保存了蛇的坐标,怎么 fwrite 进文件啊
最佳答案
2020-10-21 16:54:28
  1. #include <vector>
  2. #include <fstream>
  3. #include <iostream.h>


  4. typedef struct tagCoordinate
  5. {
  6.         int x;
  7.         int y;
  8.        
  9.         tagCoordinate(int _x,int _y)
  10.         {
  11.                 x = _x;
  12.                 y = _y;
  13.         }
  14. }COOR, *LPCOOR;

  15. using namespace std;

  16. int main(void)
  17. {
  18.         vector<COOR> vcr;

  19.         for(int i = 0 ; i < 10 ; i ++)
  20.         {
  21.                 vcr.push_back(COOR(i * 2 + 1, i * 3 + 2));
  22.         }

  23.         ofstream fWrite("e:\\snake.dat",ios::out);
  24.         typedef std::vector<COOR>::iterator VI;

  25.         for(VI vi = vcr.begin() ; vi != vcr.end() ; vi++)
  26.         {
  27.                 fWrite << vi->x << ' ' << vi->y << ' ';
  28.         }
  29.        
  30.         fWrite.close();

  31.         ifstream fRead("e:\\snake.dat",ios::in);
  32.        
  33.         COOR cr(0,0);

  34.         while(!fRead.eof())
  35.         {

  36.                 fRead >> cr.x >> cr.y;
  37.                 cout << cr.x << ',' << cr.y << endl;
  38.         }

  39.         return 0;
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-21 16:54:28 | 显示全部楼层    本楼为最佳答案   
  1. #include <vector>
  2. #include <fstream>
  3. #include <iostream.h>


  4. typedef struct tagCoordinate
  5. {
  6.         int x;
  7.         int y;
  8.        
  9.         tagCoordinate(int _x,int _y)
  10.         {
  11.                 x = _x;
  12.                 y = _y;
  13.         }
  14. }COOR, *LPCOOR;

  15. using namespace std;

  16. int main(void)
  17. {
  18.         vector<COOR> vcr;

  19.         for(int i = 0 ; i < 10 ; i ++)
  20.         {
  21.                 vcr.push_back(COOR(i * 2 + 1, i * 3 + 2));
  22.         }

  23.         ofstream fWrite("e:\\snake.dat",ios::out);
  24.         typedef std::vector<COOR>::iterator VI;

  25.         for(VI vi = vcr.begin() ; vi != vcr.end() ; vi++)
  26.         {
  27.                 fWrite << vi->x << ' ' << vi->y << ' ';
  28.         }
  29.        
  30.         fWrite.close();

  31.         ifstream fRead("e:\\snake.dat",ios::in);
  32.        
  33.         COOR cr(0,0);

  34.         while(!fRead.eof())
  35.         {

  36.                 fRead >> cr.x >> cr.y;
  37.                 cout << cr.x << ',' << cr.y << endl;
  38.         }

  39.         return 0;
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 14:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表