代码如下:
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);

CFile f;
CFileException e;
CString tpath=sPath+"\\sys1.sys";
char * pathtotal;
pathtotal=new char[tpath.GetLength()];for(int ii=0;ii<tpath.GetLength();ii++)
  pathtotal[ii]=tpath.GetAt(ii); char* pFileName =pathtotal;

    if( !f.Open( pFileName, CFile::modeRead, &e ) )
    {
#ifdef _DEBUG
        afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
    }

解决方案 »

  1.   

    char szFilePath[MAX_PATH+1];
    GetModuleFileName( NULL, szFilePath, MAX_PATH );
    char * p = strrchr( szFilePath, '\\' ) + 1;
    if( p != NULL ) *p = 0;
    strcat( szFilePath, "sys1.sys" );
    if( !f.Open( szFilePath, CFile::modeRead ) )
    {
    }
    //当然,如果sys1.sys正在被使用,就可能打不开。
      

  2.   

    为什么不使用 CreateFileEx() 函数来打开你的文件呢?这才是标准的方法嘛
      

  3.   

    用GetCurrentDirectory得到当前目录,再加上文件名,用CFile类打开
      

  4.   

    //给你正确的代码(不细心点,看不出和你原来代码的差别哦!)
    CString sPath;
    GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
    int nPos;
    nPos=sPath.ReverseFind ('\\');
    sPath=sPath.Left (nPos);

    CFile f;
    CFileException e;
    CString tpath=sPath+"\\sys1.sys";
    sPath.ReleaseBuffer ();
    char * pathtotal;
    pathtotal=new char[tpath.GetLength()];for(int ii=0;ii<tpath.GetLength();ii++)
      pathtotal[ii]=tpath.GetAt(ii);pathtotal[ii]=0; char* pFileName =pathtotal;

        if( !f.Open( pFileName, CFile::modeRead, &e ) )
        {
    #ifdef _DEBUG
            afxDump << "File could not be opened " << e.m_cause << "\n";
    #endif
        }