C++,读取文件里存储的一个类对象出错了,真心求助!!!

LL_zhuo 2012-08-13 11:19:06
我想在文件里存进去一个类对象,然后再把它读出来,结果存的时候弹出个对话框,内容如下(插入图片太麻烦了):
文件加载:
使用简体中文(GB2312)编码加载文件Material_list.bin时,
有些字节已用Unicode替换字符替换,保存该文件将不会保留原始文件内容。
然后再打开这个文件把类对象重新读出来的时候就会读出来一堆乱码,已经纠结一天了,实在是想不明白为什么了,求各位大神帮忙!
先上代码:

//date_material.h
#pragma once
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<time.h>
#include<stdio.h>
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
struct num_material
{
int num;
};
class date_material
{
char name[80];
Single price;
float density;
float Time;
public:
date_material()
{
price=0;
density=0;
Time=0;
}
date_material(String^ newDate_name,Single newDate_price,Single newDate_density)
{
Single year,month,day;
struct tm *ptr;
time_t t;
t=time(NULL);
ptr=localtime(&t);
year=ptr->tm_year+1900;
month=ptr->tm_mon+1;
day=ptr->tm_mday;
Time=year*10000+month*100+day;

char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(newDate_name).ToPointer();
for(int i=0;i<sizeof(stringPointer);++i)
name[i]=stringPointer[i];

price=newDate_price;

density=newDate_density;
}
char *Get_name()
{
return name;
}
Single Get_time()
{
return Time;
}
Single Get_price()
{
return price;
}
Single Get_density()
{
return density;
}
friend class From_selectMaterial;
}
//Form_selectMaterial.cpp
#include "date_material.h"
//输入数据
void Form_selectMaterial::add_date(date_material *date)
{
fstream outToFile;
outToFile.open("Material_list.bin",ios::binary|ios::app);
if(!outToFile)
{
MessageBox::Show("无法成功打开材料存档,请检查!","出错了",MessageBoxButtons::OK);
}
outToFile.write((char*)&date,sizeof(date_material));
outToFile.close();
}
//读取数据
void Form_selectMaterial::Get_date()
{
date_material temp;
fstream inToFile("Material_list.bin",ios::binary|ios::in);
inToFile.read((char*)&temp,sizeof(date_material));
inToFile.close();
}
...全文
329 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ybn187 2013-09-04
  • 打赏
  • 举报
回复
引用 7 楼 LL_zhuo 的回复:
我自己结贴了,在read();里面把类的指针改为类的对象的引用就可以正确读到数据了,用类的指针会提示破坏了内存,虽然还不知道为什么不可以用类的指针,不过问题解决了,谢谢前辈的回答,结贴!
贴下最终代码看看啊
LL_zhuo 2012-08-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

你使用了中文,这种事情在C++里面就比较麻烦。你有两个选择。
1、不要往文件中写中文。
2、一定要读写中文的话,必须找一个库写Unicode或者UTF-8文件。
[/Quote]
我用控制台重新编了一遍,不会弹出提示用unicode替换字符替换的这个对话框,然后输入读取就都正常了,问题应该就出在这个unicode的自动替换上,请问前辈有没有什么办法不让编译器自动用unicode替换字符来为文件内容做替换啊
LL_zhuo 2012-08-14
  • 打赏
  • 举报
回复
我自己结贴了,在read();里面把类的指针改为类的对象的引用就可以正确读到数据了,用类的指针会提示破坏了内存,虽然还不知道为什么不可以用类的指针,不过问题解决了,谢谢前辈的回答,结贴!
mirro 2012-08-13
  • 打赏
  • 举报
回复
这么多代码啊
Jim_King_2000 2012-08-13
  • 打赏
  • 举报
回复
你使用了中文,这种事情在C++里面就比较麻烦。你有两个选择。
1、不要往文件中写中文。
2、一定要读写中文的话,必须找一个库写Unicode或者UTF-8文件。
LL_zhuo 2012-08-13
  • 打赏
  • 举报
回复
刚才忘记在中间打了。。。
我的material_list.bin是空文件,就调用了一下add_date();写入了一个date_material的对象,
然后马上调用Get_date();
结果追踪数据发现读出来的是一堆乱码。
希望大神能帮帮忙~~~
顺便说一下,我用的是c++和托管C++混编的程序,不知道跟混编有没有关系。。。
LL_zhuo 2012-08-13
  • 打赏
  • 举报
回复

//date_material.h
#pragma once
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<time.h>
#include<stdio.h>
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
struct num_material
{
int num;
};
class date_material
{
char name[80];
Single price;
float density;
float Time;
public:
date_material()
{
price=0;
density=0;
Time=0;
}
date_material(String^ newDate_name,Single newDate_price,Single newDate_density)
{
Single year,month,day;
struct tm *ptr;
time_t t;
t=time(NULL);
ptr=localtime(&t);
year=ptr->tm_year+1900;
month=ptr->tm_mon+1;
day=ptr->tm_mday;
Time=year*10000+month*100+day;

char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(newDate_name).ToPointer();
for(int i=0;i<sizeof(stringPointer);++i)
name[i]=stringPointer[i];

price=newDate_price;

density=newDate_density;
}
char *Get_name()
{
return name;
}
Single Get_time()
{
return Time;
}
Single Get_price()
{
return price;
}
Single Get_density()
{
return density;
}
friend class From_selectMaterial;
}
//Form_selectMaterial.cpp
#include "date_material.h"
//输入数据
void Form_selectMaterial::add_date(date_material *date)
{
fstream outToFile;
outToFile.open("Material_list.bin",ios::binary|ios::app);
if(!outToFile)
{
MessageBox::Show("无法成功打开材料存档,请检查!","出错了",MessageBoxButtons::OK);
}
outToFile.write((char*)&date,sizeof(date_material));
outToFile.close();
}
//读取数据
void Form_selectMaterial::Get_date()
{
date_material temp;
fstream inToFile("Material_list.bin",ios::binary|ios::in);
inToFile.read((char*)&temp,sizeof(date_material));
inToFile.close();
}

LL_zhuo 2012-08-13
  • 打赏
  • 举报
回复

//date_material.h
#pragma once
#include<string>
#include<cstring>
#include<iostream>
#include<fstream>
#include<time.h>
#include<stdio.h>
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;
struct num_material
{
int num;
};
class date_material
{
char name[80];
Single price;
float density;
float Time;
public:
date_material()
{
price=0;
density=0;
Time=0;
}
date_material(String^ newDate_name,Single newDate_price,Single newDate_density)
{
Single year,month,day;
struct tm *ptr;
time_t t;
t=time(NULL);
ptr=localtime(&t);
year=ptr->tm_year+1900;
month=ptr->tm_mon+1;
day=ptr->tm_mday;
Time=year*10000+month*100+day;

char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(newDate_name).ToPointer();
for(int i=0;i<sizeof(stringPointer);++i)
name[i]=stringPointer[i];

price=newDate_price;

density=newDate_density;
}
char *Get_name()
{
return name;
}
Single Get_time()
{
return Time;
}
Single Get_price()
{
return price;
}
Single Get_density()
{
return density;
}
friend class From_selectMaterial;
}
//Form_selectMaterial.cpp
#include "date_material.h"
//输入数据
void Form_selectMaterial::add_date(date_material *date)
{
fstream outToFile;
outToFile.open("Material_list.bin",ios::binary|ios::app);
if(!outToFile)
{
MessageBox::Show("无法成功打开材料存档,请检查!","出错了",MessageBoxButtons::OK);
}
outToFile.write((char*)&date,sizeof(date_material));
outToFile.close();
}
//读取数据
void Form_selectMaterial::Get_date()
{
date_material temp;
fstream inToFile("Material_list.bin",ios::binary|ios::in);
inToFile.read((char*)&temp,sizeof(date_material));
inToFile.close();
}

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧