1.文件的打开与关闭 6.1 (3)成员函数 Close 定义: void Close() Close函数用于关闭由Open函数打开的文件。使用Open 函数打开文件后,应使用 Close函数释放文件句柄及缓冲区 的资源。 打开文件和关闭文件的例子。 CFile f: if (fOpen( D: \bak test. txt", CFile: mode Read)) Messagebox(“打开文件失败!”) return . Close
(3)成员函数Close 定义:void Close(); Close函数用于关闭由Open函数打开的文件。使用Open 函数打开文件后,应使用Close函数释放文件句柄及缓冲区 的资源。 打开文件和关闭文件的例子。 CFile f; if (!f.Open(“D:\\bak\test.txt”, CFile::modeRead)) { MessageBox(“打开文件失败!”); return ; } f.Close(); 6.1 1. 文件的打开与关闭
2文件的读写 6.1 (1)读写函数 virtual UINT Read(void* lpBuf, UINT nCount); throw(CFile Exception) Read函数返回值是传输到缓冲区的字节数。 参数 KlpBuf:指向用户提供的缓冲区以接收从文件中读取的数据。 参数 n Count:为可以从文件中读出字节数的最大值 注意:对所有 CFile类,如果到达文件尾,则返回值可能比 n Count小。 virtual void Write(const void*Ip Buf, UINT nCount); throw(CFile Exception) Write函数的参数与Read函数的参数类似。 参数 [lp Buf:指向用户提供的缓冲区,包含将写入文件中的数据。 参数 n Count:从缓冲区内传输的字节数。 Write在几种情况下均产生异常,包括磁盘满的情况、磁盘为写保护状态等。 注意:CFie类并没有提供类似EOF之类的文件结束标志,所以文件的结束是 根据Read函数的返回值来判断的。Read函数返回的是实际读出的字符数 当返回0时,则表示文件已读完
(1)读写函数 virtual UINT Read (void* lpBuf, UINT nCount); throw(CFileException); Read函数返回值是传输到缓冲区的字节数。 参数lpBuf:指向用户提供的缓冲区以接收从文件中读取的数据。 参数nCount:为可以从文件中读出字节数的最大值。 注意:对所有CFile类,如果到达文件尾,则返回值可能比nCount小。 virtual void Write(const void* lpBuf, UINT nCount); throw(CFileException); Write函数的参数与Read函数的参数类似。 参数lpBuf:指向用户提供的缓冲区,包含将写入文件中的数据。 参数nCount:从缓冲区内传输的字节数。 Write在几种情况下均产生异常,包括磁盘满的情况、磁盘为写保护状态等。 注意:CFile类并没有提供类似EOF之类的文件结束标志,所以文件的结束是 根据Read函数的返回值来判断的。Read函数返回的是实际读出的字符数, 当返回0时,则表示文件已读完。 6.1 2 文件的读写
2文件读写例题(文件复制1) 6.1 BOOL Copy(CString strSource, CString str Target) CFile fSource, f Target char C[4096];∥定义4k字节的缓冲区 nCount ∥打开文件 if (fSource Open(strSource, CFile: mode Read)) Message Box(Open Source File Fail return false: if (If Target. Open(str Target, CFile: mode Create CFile: modeWrite)) Message Box("Create Target File Fail Source. Close 0; return false
6.1 2 文件读写例题(文件复制1) BOOL Copy(CString strSource, CString strTarget) { CFile fSource, fTarget; char c[4096]; //定义4k字节的缓冲区 nt nCount; //打开文件 if (!fSource.Open(strSource, CFile::modeRead)) { MessageBox(“Open Source File Fail!”); return false; } if (!fTarget.Open(strTarget, CFile::modeCreate | CFile::modeWrite)) { MessageBox(“Create Target File Fail!”); fSource.Close(); return false; } //读文件到缓冲区c
2文件读写例题(文件复制2) 6.1 struct tegStudent Int ncode, char sName [10] int nAge; 3 STUDENT, * LPSTUDENT; BOOL Copy(CString strSource, CString str Target) CFile fSource, fTarget ∥定义 STUDENT变量 STUDENT S; nCount ∥打开文件 (IfSource Open(strSource, CFile modeRead) Message Box(Open Source File Faill) return false
6.1 2 文件读写例题(文件复制2) struct tegStudent { int nCode; char sName[10]; int nAge; } STUDENT, * LPSTUDENT; BOOL Copy(CString strSource, CString strTarget) { CFile fSource, fTarget; //定义STUDENT变量 STUDENT s; int nCount; //打开文件 if (!fSource.Open(strSource, CFile::modeRead)) { MessageBox(“Open Source File Fail!”); return false; }
3文件的定位 6.1 1) virtual LONG Seek(LONG lOff, UINT n From) throw(CFileException) 的字节偏移量。否则值未定义,并产生 CFileException 返回值:如果要求的位置合法,则Sek返回从文件开 参数O:指针移动的字节数 参数 n from:指针移动的参照点,有三种取值如下: CFle: begin:从文件开始,把指针向后移动lO字节 CFile: current:从当前位置开始,把指针向后移动lO字节。 CFile:!end:从文件尾开始,把指针向后移动lO字节。 例: CFile f: LONG OFfset= 1000. ACtual lActual =cfile Seek(OFfset, CFile: begin)
6.1 3 文件的定位 ( 1 ) virtual LONG Seek(LONG lOff, UINT nFrom); throw(CFileException); 返回值:如果要求的位置合法,则Seek返回从文件开始 的字节偏移量。否则值未定义,并产生CFileException异常。 参数lOff:指针移动的字节数。 参数nFrom:指针移动的参照点,有三种取值如下: CFile::begin:从文件开始,把指针向后移动lOff字节。 CFile::current:从当前位置开始,把指针向后移动lOff字节。 CFile::end:从文件尾开始,把指针向后移动lOff字节。 例: CFile f; LONG lOffset = 1000, lActual; lActual = cfile.Seek(lOffset,CFile::begin);