文件读写 CFile

// 打开文件(modeCreate:文件不存在就创建文件,若存在而无modeNoTruncate标志则清空文件)
CFile file( " text.txt " ,CFile::modeReadWrite | CFile::modeNoTruncate | CFile::modeCreate);
 
char  pbuf[ 100 ];
 UINT nBytesRead
= file.Read(pbuf, 100 );
 
// AfxMessageBox(pbuf);

 strcpy(pbuf,
" this file is modified by yjm " );
// 写入文件(从当前位置)
 file.Write(pbuf, sizeof (pbuf));
 
// AfxMessageBox(pbuf);
 file.Close();
文件定位:
virtual  LONG  Seek(LONG lOff,UINT nFrom);
作用:把文件位置指针相对移动一个偏移量;
参数:
nForm:标志移动的基准,可以取CFile::begin 
/  CFile::current  /  CFile::end,分别表示相对于文件头、当前位置、文件尾;
IOff:偏移量(可正可负)

LONG IOffset
= 1000 ,IActual;
IActual
= file.Seek(IOffset,CFile::begin)
// 指针相对文件头向后移动1000字节
其他函数:
SeekToBegin
SeekToEnd
GetPosition//得到当前文件指针位置
GetLength //得到文件长度
GetStatus // 得到文件属性
...

你可能感兴趣的:(File)