具体问题是:现有一个文件,我想先用CFileDialog打开,然后我需要把打开文件中的一部分读入一个定义好的struct中,代码如下:
//******************************************************
void CC1a5View::rectif()
{
CC1a5Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString FilePathname;
CString FileName;
CDC*pDC=GetDC();
CFile MyFile;
CFileDialog dlg( TRUE,_T("dat"),_T("*.dat"),
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("dat文件 (*.dat)|*.dat|"));
if(IDOK==dlg.DoModal())
{
FilePathname.Format("%s %s","filepath:",dlg.GetPathName());
FileName.Format("%s %s","Old file name:",dlg.GetFileName());
MyFile.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);
}
pDC->TextOut(0,0,FileName);
pDC->TextOut(0,20,FilePathname);
pDC->TextOut(0,40,"文件被打开");
MyFile.Close(); md=&mhead1;
if(!MyFile.Open(dlg.GetFileName(), CFile::modeReadWrite))
{
AfxMessageBox("can't open the dat file");
return;
} MyFile.Seek(sizeof(mainhead),CFile::begin);
MyFile.Read(md,sizeof(mainhead));}
其中mainhead是我已经定义好的struct,如下
struct mainhead //数据头记录
{ unsigned short sat_lab;
unsigned short begin_year;
unsigned long begin_msecond;
unsigned short begin_days;;
unsigned short end_year;
unsigned long end_msecond;
unsigned short end_days;
}mhead1,*md;
//*************************************************************我的问题是我不知道为什么我在调试中看md始终是没有真正的读入要读入的内容,我是在哪个环节出了问题呢?
请各位帮忙看看,能给我举个实例最好了,先谢过了
//******************************************************
void CC1a5View::rectif()
{
CC1a5Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString FilePathname;
CString FileName;
CDC*pDC=GetDC();
CFile MyFile;
CFileDialog dlg( TRUE,_T("dat"),_T("*.dat"),
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("dat文件 (*.dat)|*.dat|"));
if(IDOK==dlg.DoModal())
{
FilePathname.Format("%s %s","filepath:",dlg.GetPathName());
FileName.Format("%s %s","Old file name:",dlg.GetFileName());
MyFile.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);
}
pDC->TextOut(0,0,FileName);
pDC->TextOut(0,20,FilePathname);
pDC->TextOut(0,40,"文件被打开");
MyFile.Close(); md=&mhead1;
if(!MyFile.Open(dlg.GetFileName(), CFile::modeReadWrite))
{
AfxMessageBox("can't open the dat file");
return;
} MyFile.Seek(sizeof(mainhead),CFile::begin);
MyFile.Read(md,sizeof(mainhead));}
其中mainhead是我已经定义好的struct,如下
struct mainhead //数据头记录
{ unsigned short sat_lab;
unsigned short begin_year;
unsigned long begin_msecond;
unsigned short begin_days;;
unsigned short end_year;
unsigned long end_msecond;
unsigned short end_days;
}mhead1,*md;
//*************************************************************我的问题是我不知道为什么我在调试中看md始终是没有真正的读入要读入的内容,我是在哪个环节出了问题呢?
请各位帮忙看看,能给我举个实例最好了,先谢过了
解决方案 »
- 谁有利用VS2005将数据导到word文档的程序啊
- 用FindFirstFile()和FindNextFile()查找文件,如何知道找到的东西是个文件还是个文件夹?
- 急!!!
- list contrl控件中哪个函数可以定位到指定行,就像list 中m_list.SetCurSel(nCurSel);函数功能一样?
- (急救!!)去掉浏览器上滚动条的问题
- 我很急的问题,在试用其间
- IE浏览器开发问题,请高手指点。我全部分都给出了。。。帮忙,谢谢。
- 关于在一个程序中调用另一个程序的问题!!!
- 退出对话框时中止线程,似乎不容易
- 关于ATL组件连接点的问题,还请各位高指点一下。非常感谢
- new的指针类成员如何在不同消息处理函数间共享?
- ****在线等待,关于编辑框显示的问题*****???????????????????
MyFile.Seek(0,CFile::begin);
CFileDialog dlg( TRUE,_T("dat"),_T("*.dat"),
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("dat文件 (*.dat)|*.dat|"));
if(IDOK==dlg.DoModal())
{
MyFile.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
struct mainhead mh;
mh.sat_lab = 9;
//write other data..
MyFile.Write((const void*)&mh, sizeof(struct mainhead));
MyFile.Close(); //begin read a structure
MyFile.Open(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
struct mainhead *stRead = new mainhead;
MyFile.Read((void*)stRead, sizeof(struct mainhead));
MyFile.Close();
}
//调试通过。稍微修改一下,就可以循环写入和读取了。
我需要读入的是用CFileDialog打开的那个.dat文件的一部分,即将.dat文件的开始部分读入到已经定义好的struct mainhead中,而不是自己写mainhead中的数据。请你帮忙再看看好吗?
谢谢了! :)
条件语句包括到你要写的程序完