在windows系统中以二进制的方式读取二进制文件时如何判断文件是否结束。
例如:
     FILE * fp=fopen(filename,"rb");
程序中如何判断是否结束?

解决方案 »

  1.   

    你首先要知道这个文件有多长,CFile::GetLength()在你读的时候Read( void* lpBuf, UINT nCount );
     nCount如果累积到了GetLength()的长度就结束了。
      

  2.   

    // set up overlapped structure fields 
    // to simplify this sample, we'll eschew an event handle 
    gOverLapped.Offset     = 0; 
    gOverLapped.OffsetHigh = 0; 
    gOverLapped.hEvent     = NULL; 
     
    // attempt an asynchronous read operation 
    bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, 
        &gOverlapped) ; 
     
    // if there was a problem, or the async. operation's still pending ... 
    if (!bResult) 

        // deal with the error code 
        switch (dwError = GetLastError()) 
        { 
            case ERROR_HANDLE_EOF: 
            { 
                // we're reached the end of the file 
                // during the call to ReadFile 
     
                // code to handle that 
            } 
     
            case ERROR_IO_PENDING: 
            { 
                // asynchronous i/o is still in progress 
     
                // do something else for a while 
                GoDoSomethingElse() ; 
     
                // check on the results of the asynchronous read 
                bResult = GetOverlappedResult(hFile, &gOverlapped, 
                    &nBytesRead, FALSE) ; 
     
                // if there was a problem ... 
                if (!bResult) 
                { 
                    // deal with the error code 
                    switch (dwError = GetLastError()) 
                    { 
                        case ERROR_HANDLE_EOF: 
                        { 
                            // we're reached the end of the file 
                            //during asynchronous operation 
                        } 
     
                        // deal with other error cases 
                    } 
                } 
            } // end case 
     
            // deal with other error cases 
     
        } // end switch 
    } // end if 
      

  3.   

    真受不了了,这个问题居然有扛4个三角的这样答.................if ( feof(fp) )//文件结束
    {  
       fclose(fp)
    }
      

  4.   

    To:fangcheng(风清云淡) ( ) Win32编程、你这样做安全吗?说实在的,在进入Win32下,提供的IO能力有限,很不方便。
      

  5.   

    FILE * fp=fopen(filename,"rb");看清楚,他是以这种方式打开的