抄段代码给你
    if ((hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, 
            OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL)) == (HANDLE)-1) { 
        wsprintf(text, GetStringRes(IDS_FMT_FILEOPNFAIL), GetLastError()); 
        MessageBox(ghwndMain, text, GetStringRes(IDS_ERROR), MB_OK); 
        return 0L; 
    } 
 
    // 
    // Create a map file of the opened file 
    // 
    if ((hMapFile = CreateFileMapping(hFile, NULL, 
                             PAGE_READONLY, 0, 0, "MapF")) == NULL) { 
        wsprintf(text, GetStringRes(IDS_FMT_CREMAPFILEFAIL), GetLastError()); 
        MessageBox(ghwndMain, text, GetStringRes(IDS_ERROR), MB_OK); 
        bSuccess = FALSE; 
        goto ErrorExit1; 
    } 
 
    // 
    // Map a view of the whole file 
    // 
    if ((pMapFile = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0)) == NULL) { 
        wsprintf(text, GetStringRes(IDS_FMT_MAPVWMAPFOFAIL), GetLastError()); 
        MessageBox(ghwndMain, text, GetStringRes(IDS_ERROR), MB_OK); 
        bSuccess = FALSE; 
        goto ErrorExit2; 
    } 
 
    // 
    // First check that if it is an enhanced metafile 
    // 
    pemh = (LPENHMETAHEADER) pMapFile; 
    if (pemh->dSignature == META32_SIGNATURE) { 
        hemf = GetEnhMetaFile(szFile); 
        goto HLM_EXIT; 
    } 
 

解决方案 »

  1.   

    不用这么麻烦吧,建立一个cin对象不就行了么
      

  2.   

    不不,不是cin对象,是流文件对象
      

  3.   

    You can use CMemFile;Example ://for read only
          CFile file;
          file.Open("FileName");
          BYTE* pBuf = new byte[file.GetLength()];
          file.Read(pBuf,file.GetLength());
          file.Close();
          CMemFile memFile;
          memFile.Attch(pBuf,file.GetLength());
          .
          .
          memFile.Detach();
          delete[] pBuf;
      

  4.   

    用VC下的api,ReadFile也可以直接自己定义 ifstream 定义自己 的输入文件流
      

  5.   

    把文件读入内存后,怎么使用此文件?(假设文件名为thanks)
      

  6.   

    给你个简单的类
    要文件时调用GetFile()class CCacheFile{
    public:
    BOOL AddFileToCache(CString strFileName);//缓冲指定文件
    CString GetFile(CString strFileName); //取文件内容
    CCacheFile(){
    LegalPostfixList[0] = CString("htm"); //允许客户端索取的文件类型
    LegalPostfixList[1] = CString("html");
    LegalPostfixList[2] = CString("js");
    LegalPostfixList[3] = CString("gif");
    LegalPostfixList[4] = CString("jpg");
    LegalPostfixList[5] = CString("swf");
    LegalPostfixList[6] = CString("zip"); AddFileToCache("default.htm");
    AddFileToCache("post.htm");
    AddFileToCache("display.htm");
    AddFileToCache("nameList.htm");
    // AddFileToCache("error.htm");
    }
    private:
    CString strFileList[5];
    CString strFileContentCache[5];
    CString LoadDiskFile(CString strFileName);
    bool IsRequestLegal(CString strFileName);
    CString LegalPostfixList[20];
    };
    BOOL CCacheFile::AddFileToCache(CString strFileName)
    {
    strFileName.MakeLower();
    for(int i=0;i<5;i++)
    if("" == strFileList[i]){
    strFileContentCache[i] = LoadDiskFile(strFileName);
    if("" != strFileContentCache[i])
    strFileList[i] = strFileName;
    return true;
    }
    return false;
    }
    CString CCacheFile::GetFile(CString strFileName)
    {
    if("" == strFileName) return "";
    strFileName.MakeLower();
    for(int i=0;i<5;i++){
    if(strFileList[i] == strFileName)
    return strFileContentCache[i];
    }
    return LoadDiskFile(strFileName);
    }
    CString CCacheFile::LoadDiskFile(CString strFileName)
    {
    CString strFileBuf;
    CFile myFile; if("" == strFileName) return "";
    if(! IsRequestLegal(strFileName)){
    cerr << "not legal request :" << strFileName << endl; 
    return "";
    }
    char dir[1000];
    GetCurrentDirectory(sizeof(dir) , dir);
    CString tmp = dir;
    strFileName = tmp + "\\" + strFileName;
    // strFileName = "E:\\HttpServer\\debug\\" + strFileName;
    if(!myFile.Open(strFileName,CFile::modeRead | CFile::shareDenyNone))
    {
    cerr << _T("faile to open file ") << strFileName << endl;
    return "";
    }
    char cbBuffer[101];
    int nFileSize = myFile.GetLength();
    int nBufSize = sizeof(cbBuffer) - 1; try{
    while(nFileSize>0){
    if(nFileSize < nBufSize)
    nBufSize = nFileSize;
    int nBytesRead = myFile.Read(cbBuffer,nBufSize);
    if(nBytesRead>0){
    cbBuffer[nBytesRead] =  0x00;
    strFileBuf += cbBuffer;
    }else
    break;
    nFileSize -= nBufSize;
    }
    }catch(CFileException *e){
    cerr << _T("faile to read file ") << strFileName << endl;
    e->Delete();
    return "";
    }
    return strFileBuf;
    }
    bool CCacheFile::IsRequestLegal(CString strFileName)
    {
    UINT n = strFileName.Find('.');
    if(n<0) return false;
    CString strPostfix = strFileName.Right(strFileName.GetLength()-n-1);
    strPostfix.MakeLower();
    if("" == strPostfix) return false;
    for(int i=0;i<7;i++)
    if(NULL != LegalPostfixList[i])
    if(strPostfix == LegalPostfixList[i])
    return true;
    return false;
    }