前面介紹了標準C++和C Run-Time Library中檔案操作的方式,缺點顯而易見,很多的東西和驗證需要我們自己進行。
在windows中,系統封裝了這些運行庫,然後給了我們幾個可供使用的API來操作檔案。
同樣的,windows的SDK大多是面向過程的封裝。下面直接上代碼:
讀檔案:
char filename[50];<br />cout<<"Pleast input the file name you want to Read:/n";<br />cin>>filename;<br />WCHAR wszUserName[256]; // Unicode user name<br />// Convert ANSI user name and domain to Unicode<br />MultiByteToWideChar( CP_ACP, 0, filename,<br />strlen(filename)+1, wszUserName,<br />sizeof(wszUserName)/sizeof(wszUserName[0]) );<br />HANDLEhFile;<br />DWORDdwFileSize;<br />DWORDdwBytesRead;<br />hFile = CreateFile(wszUserName, // file to open<br />GENERIC_READ, // open for reading<br />FILE_SHARE_READ, // share for reading<br />NULL, // default security<br />OPEN_EXISTING, // existing file only<br />FILE_ATTRIBUTE_NORMAL, // normal file<br />NULL); // no attr. template<br />if (hFile == INVALID_HANDLE_VALUE)<br />{<br />cout<<"Could not open file (error :)/n"<< GetLastError();<br />return;<br />}<br />dwFileSize = GetFileSize(hFile, NULL);<br />char *strInfo = new char[dwFileSize + sizeof(char)];<br />memset( strInfo, 0, ( dwFileSize + sizeof(char)) *sizeof(char) );// 清空<br />if(!ReadFile(hFile, strInfo, dwFileSize, &dwBytesRead, NULL))<br />{<br />cout<<"Could not read from file (error :)/n"<<GetLastError();<br />return;<br />}<br />int len = strlen(strInfo);<br />for (int i = 0; i < len; ++i)<br />{<br />cout<<strInfo[i];<br />}</p><p>CloseHandle(hFile);<br />delete []strInfo;
寫檔案:
cout<<"Pleast input the file name you want to Write:/n";<br />char filename[50];<br />cin>>filename;<br />WCHAR wszUserName[256]; // Unicode user name<br />// Convert ANSI user name and domain to Unicode<br />MultiByteToWideChar( CP_ACP, 0, filename,<br />strlen(filename)+1, wszUserName,<br />sizeof(wszUserName)/sizeof(wszUserName[0]) );<br />DWORD dwResult;<br />HANDLE hFile;<br />hFile = CreateFile(wszUserName, // name of the write<br />GENERIC_WRITE, // open for writing<br />0, // do not share<br />NULL, // default security<br />CREATE_ALWAYS, // overwrite existing<br />FILE_ATTRIBUTE_NORMAL, // normal file<br />NULL); // no attr. template<br />if (hFile == INVALID_HANDLE_VALUE)<br />{<br />cout<<"Could not open file (error :)"<< GetLastError();<br />return;<br />}<br />cout<<"Please input the data you want to write:/n";<br />char inputData[100];<br />while(cin>>inputData)<br />{<br />if (inputData[0] == 'q')<br />{<br />CloseHandle(hFile);<br />return;<br />}<br />if(!WriteFile (hFile, inputData, strlen(inputData), &dwResult, NULL))<br />{<br />cout<<"Could not write to file (error:)" << GetLastError();<br />return;<br />}<br />if(!WriteFile (hFile, "/r/n", strlen("/r/n"), &dwResult, NULL))<br />{<br />cout<<"Could not write to file (error:)" << GetLastError();<br />return;<br />}<br />}</p><p>CloseHandle(hFile);<br />
範例程式碼同樣簡單,不過需要注意的是他們的參數,參數的意義可以參考MSDN的說明,上面有非常詳細的描述和例子。
由於我的工程屬性是Unicode編碼,所以在其中,我使用了
WCHAR wszUserName[256]; // Unicode<br />MultiByteToWideChar( CP_ACP, 0, filename,<br />strlen(filename)+1, wszUserName,<br />sizeof(wszUserName)/sizeof(wszUserName[0]) );
來轉換編碼,當然,你也可以根據你自己的工程環境來進行修改,你還可以使用這寫函數的ANSI版本或是Unicode版本,使用方式和參數
與上面相同,如CreateFileW
(Unicode) 和CreateFileA
(ANSI).對於這些函數的具體描述,請參看MSDN上的說明。
附,本系列範例程式碼
,該代碼在VS2008+XPsp3下測試通過。