鱼C论坛

 找回密码
 立即注册
查看: 2490|回复: 3

0xfeeefeee错误

[复制链接]
发表于 2018-6-9 11:42:04 | 显示全部楼层 |阅读模式
50鱼币
本帖最后由 Sj中国智造 于 2018-6-9 12:19 编辑
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>

  4. using namespace std;

  5. class ceshi
  6. {
  7. public:
  8.         map<int,int> obm;
  9.         int count;
  10. };

  11. int main()
  12. {
  13.         ceshi c,c1;
  14.         c.count = 0;
  15.         c.obm[100] = 10;

  16.         ofstream out("1.dat",ios::out|ios::binary);
  17.         out.write((char*)&c,sizeof(c));

  18.         out.close();
  19.         ifstream in("1.dat",ios::in|ios::binary);

  20.         in.read((char*)&c1,sizeof(c1));
  21.         cout <<c1.obm[100]<<endl;
  22.         cout <<&(c1.obm)<<endl;
  23.         cout <<&(c.obm)<<endl;
  24.         in.close();

  25.         system("pause");
  26.         return 0;
  27. }
复制代码

简化版代码
求大神解啊,这是我们大作业中的一部分,可有偿,这个程序在vs2010收尾时不成功,在codeblocks下正常。急,求解决方法

无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-9 13:17:22 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>

  3. int main()
  4. {
  5.         std::string s1("1234");
  6.         std::string s2;
  7.         memcpy(&s2, &s1, sizeof(s1));

  8.         std::cout << s1 << std::endl;
  9.         std::cout << s2 << std::endl;

  10.         return 0;
  11. }
复制代码


你这个程序和我写的这个有什么区别?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-9 13:30:22 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>

  3. class String
  4. {
  5. public:
  6.         String()
  7.         {
  8.                 this->data = NULL;
  9.         }
  10.         String(const char *s)
  11.         {
  12.                 this->data = new char[strlen(s) + 1];
  13.                 strcpy(this->data, s);
  14.         }
  15.         ~String()
  16.         {
  17.                 if(this->data)
  18.                         delete[] this->data;
  19.         }

  20.         String &operator=(const String &r)
  21.         {
  22.                 this->data = new char[strlen(r.data) + 1];
  23.                 strcpy(this->data, r.data);
  24.                 return *this;
  25.         }

  26.         friend std::ostream &operator<<(std::ostream &l, const String &r)
  27.         {
  28.                 l << r.data;
  29.                 return l;
  30.         }
  31. private:
  32.         char *data;

  33. };

  34. int main()
  35. {
  36.         String s1("1234");
  37.         String s2;

  38.         s2 = s1;
  39.         std::cout << s1 << std::endl;
  40.         std::cout << s2 << std::endl;

  41.         String s3("4567");
  42.         String s4;
  43.         memcpy(&s4, &s3, sizeof(String));        // 仔细想一想,这会发生什么问题?
  44.         std::cout << s3 << std::endl;
  45.         std::cout << s4 << std::endl;

  46.         return 0;
  47. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-9 15:37:08 | 显示全部楼层
cpp的fstream并没有足够智能到足以保存类对象的的地步,所以你要么自己实现,要么借助第三方类库,下面我帮你写个一个比较简单的方法,你拿去吧

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>

  5. using namespace std;

  6. class ceshi{
  7. public:
  8.     map<int, string> obm;
  9.     int count;
  10.     friend ifstream& operator>>(ifstream& is, ceshi& st);
  11.     friend ofstream& operator<<(ofstream& os, const ceshi& st);
  12. };

  13. ifstream& operator>>(ifstream& is, ceshi& st){
  14.     is >> st.count;
  15.    
  16.     //如果map对象有多个的话,这里可以写个循环来循环写入
  17.     int first;
  18.     string second;
  19.     is >> first;
  20.     is >> second;

  21.     st.obm.insert(pair<int, string>(first, second));
  22.     return is;
  23. }

  24. ofstream& operator<<(ofstream& os, ceshi& st){
  25.     os << st.count << " ";
  26.     for (auto iter = st.obm.begin(); iter != st.obm.end(); iter++) {
  27.         os << iter->first << " " << iter->second << " ";
  28.     }
  29.     return os;
  30. }


  31. int main() {
  32.     ceshi c;
  33.     c.count = 6;
  34.     c.obm[44] = "gdagzsd";
  35.     ofstream fout("ceshi.txt");
  36.     fout << c;
  37.     fout.close();

  38.     ceshi c2;
  39.     ifstream fin("ceshi.txt");
  40.     fin >> c2;

  41.     cout << c2.count << endl;
  42.     for (auto iter = c2.obm.begin(); iter != c2.obm.end(); iter++){
  43.         cout << iter->first << " " << iter->second << endl;
  44.     }
  45.     fin.close();




  46.     return 0;
  47. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 21:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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