只要知道文件路径就可以进行文件的读取,所有只要传递文件名 char* pszFileName就可以了;
int CPlaybill::Save(const char* pszFileName)
{
int nRet;
int nDataLen;
BYTE *pBuf;
FILE *fp;
nRet = 0;
nDataLen = GetDataLength();
pBuf = (BYTE*)malloc(nDataLen);
if(pBuf)
{
fp = fopen(pszFileName, "wb");
if(fp)
{
DataToBuffer(pBuf);
fwrite(pBuf, nDataLen, 1, fp);
fclose(fp);
nRet = nDataLen;
}
else
{
nRet = -2;
}
free(pBuf);
}
else
{
nRet = -1;
}
return nRet;
}
读取文件:
BYTE CPlaybill::GetProgramWinCnt(const char* pszFileName)
{
BYTE byWinCnt;
CFile file;
BYTE szBuf[10];
byWinCnt = 0;
if(file.Open(pszFileName, CFile::modeRead|CFile::typeBinary|CFile::shareDenyNone))
{
memset(szBuf, 0x00, sizeof(szBuf));
file.Read(szBuf, 10);
byWinCnt = szBuf[4];
file.Close();
}
return byWinCnt;
}
用对话框的方式打开文件并获取文件内容
CFileDialog filedlg( TRUE );
filedlg.m_ofn.lpstrFilter="txt(*.txt)\0*.txt";
filedlg.m_ofn.lpstrDefExt = "txt";
CString FileName;
FileName = GetFiledName();
filedlg.m_ofn.lpstrInitialDir= FileName ;
if(IDOK==filedlg.DoModal())
{
CStdioFile file;
CString strLine = "" , strtemp ;
if(file.Open( filedlg.GetPathName() , CFile::modeRead |CFile::typeBinary ))//打开*.TXT这个文件,打开形式为读取
{
if( file.GetLength() > 0)
{
SetDlgItemText( IDC_EDIT_HISTORY , "" );
while(file.ReadString( strtemp ) )
{
strLine += strtemp + "\r\n";
}
file.Close();
UpdateData(false);
SetDlgItemText( IDC_EDIT_HISTORY , strLine );
m_EditHistory.LineScroll( m_EditHistory.GetLineCount() );
}
}
}