结构如下:
typedef struct info_Novel
{
string url;
string src;
string title;
}InfoNovel;
vector<InfoNovel> Info;
请问如何将Info,写入dat文件及从dat文件中读取出来??
typedef struct info_Novel
{
string url;
string src;
string title;
}InfoNovel;
vector<InfoNovel> Info;
请问如何将Info,写入dat文件及从dat文件中读取出来??
解决方案 »
- winxp中用CFileDialog如何选择上万文档?
- SDI 调试不好用了 帮忙!!
- 请问如何删除错误添加的类?
- error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
- 画线的问题,不论背景什么颜色,画出来的临时线就是你选择的颜色,和背景无关。
- 问一个关于多线程的问题!!请进来看看
- 在Edit控件中,如何用OnChar来扑捉汉字
- 在那里能下載最新的Win32 SDK,微軟網站好像不行!祇能讓你安裝.還裝不上.
- 没有头绪!!!!!----win98下面在程序里面如何启动拨号网络服务器?
- 关于图象文件保存格式问题,求教高手???
- 菜鸟问题:如何在对话框上显示一个带有超级链接的图片?
- CWebPage的用法
两种分隔符
一种用来分开struct成员
一种用来分开vector成员
typedef struct _st_dat
{
int nx;
float fy;
char szBuf[12];
}ST_DAT;void MyWrite(CString strFile, const vector<ST_DAT> &vDat)
{
HANDLE hFile = CreateFile(strFile, FILE_WRITE_DATA, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return;
}
long lLen = vDat.size();
DWORD dwWrite = 0;
BOOL bRet = FALSE;
// 先定长度
// 12 是随便写的一个偏移,你可能要根据具体的要求具体写
bRet = WriteFile(hFile, &lLen, 12, &dwWrite, NULL);
// 移动文件指针
SetFilePointer(hFile, 12, NULL, FILE_BEGIN);
// 再写内容
bRet = WriteFile(hFile, &vDat, sizeof(vDat) * lLen, &dwWrite, NULL);
CloseHandle(hFile);
}
void MyRead(CString strFile, vector<ST_DAT> &vDat)
{
long lLen = 0;
HANDLE hFile = CreateFile(strFile, FILE_READ_DATA, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return;
}
BOOL bRet = 0;
DWORD dwRead = 0;
// 12 是随便写的一个偏移,你可能要根据具体的要求具体写
bRet = ReadFile(hFile, &lLen, 12, &dwRead, NULL);
vDat.resize(lLen);
SetFilePointer(hFile, 12, NULL, FILE_BEGIN);
bRet = ReadFile(hFile, &vDat, lLen * sizeof(ST_DAT), &dwRead, NULL);
CloseHandle(hFile);
}
void testmydat()
{
// TODO: 在此添加控件通知处理程序代码
// OnOK();
ST_DAT st;
st.fy = 1.23f;
st.nx = 2;
strcpy(st.szBuf, "abd");
vector<ST_DAT> vs;
vs.push_back(st);
st.fy = 2.34f;
st.nx = 99;
strcpy(st.szBuf, "xyz");
vs.push_back(st); CString strFile = TEXT("c:/mydat.dat");
MyWrite(strFile, vs);
vector<ST_DAT> vrtest;
MyRead(strFile, vrtest);
}
bRet = WriteFile(hFile, &lLen, 12, &dwWrite, NULL);
这个偏移量12,是指什么
不好意思,这个函数没用过,自己查费时间
查个MSDN一分钟而已,比发贴的时间少多了. 这两个函数随手写的,功能都应该能实现,你可能要自己调试一下。
重新打开程序读的时候,就会挂掉:MSVCP60D.DLL 越界
vector<InfoNovel>::iterator itr_Begin=vInfo.begin();
vector<InfoNovel>::iterator itr_End=vInfo.end();
vector<InfoNovel>::iterator itr_Iter;
size_t test=vInfo.size();
for (itr_Iter=itr_Begin;itr_Iter!=itr_End;++itr_Iter)
{
CString strUrl=itr_Iter->url.c_str();//挂在这
CString strTitle=itr_Iter->title.c_str();
CString strSrc=itr_Iter->src.c_str();
}
这个是怎么回事。
把上面代码里面的 ReadFile/WriteFile 的第二个参数由原来的 &vDat 改写成 &vDat[0] 就可以了。我自己测试过是可以的。