看看CFile的使用

   
     
1 /* 该函数用来将文件中所有的大写字母转化为小写字母,但是不支持UNICODE */
2 #include < afx.h >
3 // #include <iostream>
4 // using namespace::std;
5
6 int main()
7 {
8 BYTE buffer[ 0x100 ];
9 try
10 {
11 CFile file(L " File2.txt " , CFile::modeReadWrite);
12 ULONGLONG dwBytesRemaining = file.GetLength();
13 while (dwBytesRemaining)
14 {
15 ULONGLONG dwPosition = file.GetPosition();
16 UINT nBytesRead = file.Read(buffer, sizeof (buffer));
17 ::CharLowerBuff((LPWSTR)buffer,nBytesRead);
18 file.Seek(dwPosition,CFile::begin);
19 file.Write(buffer,nBytesRead);
20 dwBytesRemaining -= nBytesRead;
21 }
22 file.Close();
23 }
24 catch (CFileException * e)
25 {
26 // cout<<"Third: Failed!"<<endl;
27 e -> ReportError();
28 e -> Delete();
29 }
30
31 return 0 ;
32 }
  
    
/*本人闲暇之余学习MFC的CFile类,在学习的过程中,写了如下的程序进行练习,贴出来和大家共享, Enjoy it.
------cylee
*/
1
#include < afx.h >
2 #include < iostream >
3 using namespace ::std;
4
5 int main()
6 {
7 CFile file;
8 CFileException err;
9
10
11 // The following code try to open a file named File.txt,
12 // It will fail unless the file is located in the the current directoty,
13 // because the Open member function open files only.
14 // The open member function doesn't throw any exception, so you can
15 // not use try-catch mechanism to catch errors, but you can do it in another way.
16
17 if ( file.Open(_T( " File.txt " ),CFile::modeReadWrite, & err))
18 cout << " First: Succeeded! " << endl;
19 else
20 err.ReportError();
21 file.Close();
22
23 // Use constructor to open a file without creation, too.
24 // an exception is throwed when an error accured.
25 try
26 {
27 CFile file2(_T( " File.txt " ), CFile::modeReadWrite);
28 cout << " Second: Succeeded! " << endl;
29 }
30 catch (CFileException * e)
31 {
32 cout << " Second: Failed! " << endl;
33 e -> ReportError();
34 e -> Delete();
35 }
36
37 // Create and Open a file in only one statement.
38 }

你可能感兴趣的:(File)