我要使用fread(void *,int ,int,FILE *)读取一个文件。fread的第二和第三参数决定了读取的大小。可我怎么获得我要读取的文件的长度啊?如果随便写个很大的数,会不会出毛病啊。如何精确点?

解决方案 »

  1.   

    CFile::GetLength 
    virtual DWORD GetLength( ) const;
    throw( CFileException );Return ValueThe length of the file.ResObtains the current logical length of the file in bytes, not the amount.CFile Overview |  Class Members |  Hierarchy ChartSee Also   CFile::SetLength
      

  2.   

    #include <sys\stat.h> FILE *file;
    char str[]="E:\\1.htm";
    file=fopen(str,"r");
    struct stat st; 
    if(stat(str,&st))cout<<"读取出错!\n"; 
    int iFileSize = st.st_size;  //获取文件大小
    char *buf = new char[iFileSize+1];
    memset(buf,'\0',iFileSize+1);
    fread( buf, sizeof(char), iFileSize, file );
    cout<<buf<<'\n'<<strlen(buf)<<'\n';
    delete buf;
      

  3.   

    char buff[1024];int nLength = fread( list, sizeof( char ), 1024, stream );     //第三个参数指定最大可读缓冲区大小,返回的是文件所读取的内容长度buff[nLength] = '\0';      //之后加个'\0'结尾就OK了
      

  4.   

    char buff[1024];int nLength = fread( buff, sizeof( char ), 1024, stream );     //第三个参数指定最大可读缓冲区大小,返回的是文件所读取的内容长度buff[nLength] = '\0';      //之后加个'\0'结尾就OK了