写了如下的一段vc代码,本来是准备使用Seek在文件中随机定位,然后读出相应位置上的float数据的,测试的时候,却发现和想象的结果相差很远。
CFile file;
float *m_Data=new float[tmpRow];
file.Open(filename, CFile::modeRead);
for(int j=0;j {
file.Seek(j, CFile::begin );
file.Read(tmpval, sizeof(float));
m_Data[j]=*tmpval;
}
苦苦思考了一下午。 仔细阅读了MSDN中的说明,方才搞清楚原来应该使用
file.Seek(j*sizeof(float), CFile::begin );
即Seek( LONG lOff, UINT nFrom )中的lOff是以byte为单位的,而不是自动根据数据格式调整。
把MSDN中的一段附在后面,立此存照。
唉,教训啊,开始的时候没有仔细看清楚。
CFile::Seek
virtual LONG Seek( LONG lOff, UINT nFrom );
throw( CFileException );
Return Value
If the requested position is legal, Seek returns the new byte offset from the beginning of the file. Otherwise, the return value is undefined and a CFileException object is thrown.
Parameters
lOff
Number of bytes to move the pointer.
nFrom
Pointer movement mode. Must be one of the following values:
- CFile::begin Move the file pointer lOff bytes forward from the beginning of the file.
- CFile::current Move the file pointer lOff bytes from the current position in the file.
- CFile::end Move the file pointer lOff bytes from the end of the file. Note that lOff must be negative to seek into the existing file; positive values will seek past the end of the file.
阅读(5230) | 评论(0) | 转发(0) |