CreateFile函數(shù)詳解.....WriteFile和ReadFile
(2011-01-13 09:42:06)
標(biāo)簽:
雜談Windows API一日一練(54)WriteFile和ReadFile函數(shù)
讀寫(xiě)文件是每個(gè)Windows軟件開(kāi)發(fā)人員都需要做的工作。可見(jiàn)這項(xiàng)工作是非常重要的,畢竟各種各樣的數(shù)據(jù)都需要保存起來(lái),以便作各種各樣的分析,或者通過(guò)網(wǎng)絡(luò)傳送給別人。像大家用BT下載的電影,在那個(gè)BT軟件里,就需要不斷從網(wǎng)絡(luò)里接收到數(shù)據(jù),然后再把這些數(shù)據(jù)保存到文件里合適的位置,就可以生成跟發(fā)行者那里一樣的文件,這樣才可以播放出來(lái)。又比如我在玩《征途》的游戲里,剛剛打開(kāi)游戲時(shí),它就不斷從服務(wù)器上下載更新的文件下來(lái),然后保存到硬盤(pán)。WriteFile函數(shù)是用來(lái)寫(xiě)數(shù)據(jù)到文件,ReadFile函數(shù)是從文件里讀取數(shù)據(jù)出來(lái)。但這兩個(gè)函數(shù)不但可以讀取寫(xiě)磁盤(pán)的文件,也可以接收和發(fā)送網(wǎng)絡(luò)的數(shù)據(jù),還有讀寫(xiě)串口、USB、并口等設(shè)備的數(shù)據(jù)。在讀寫(xiě)文件里,首先就是先打開(kāi)文件,然后判斷打開(kāi)是否成功。在寫(xiě)文件時(shí),同時(shí)要注意磁盤(pán)的空間是否滿(mǎn)等問(wèn)題。在讀取文件時(shí),往往需要讀取不同位置的文件,比如要讀取一個(gè)4G的視頻文件,就不可能完全把它讀取到內(nèi)存里,因此就需要對(duì)文件進(jìn)行定位讀取。
函數(shù)WriteFile和ReadFile聲明如下:
WINBASEAPI
BOOL
WINAPI
WriteFile(
__in HANDLE hFile,
__in_bcount(nNumberOfBytesToWrite) LPCVOID lpBuffer,
__in DWORD nNumberOfBytesToWrite,
__out_opt LPDWORD lpNumberOfBytesWritten,
__inout_opt LPOVERLAPPED lpOverlapped
);
WINBASEAPI
BOOL
WINAPI
ReadFile(
__in HANDLE hFile,
__out_bcount_part(nNumberOfBytesToRead, *lpNumberOfBytesRead) LPVOID lpBuffer,
__in DWORD nNumberOfBytesToRead,
__out_opt LPDWORD lpNumberOfBytesRead,
__inout_opt LPOVERLAPPED lpOverlapped
);
hFile是文件句柄。
lpBuffer是讀寫(xiě)數(shù)據(jù)緩沖區(qū)。
nNumberOfBytesToWrite是多少數(shù)據(jù)要寫(xiě)入。
lpNumberOfBytesWritten是已經(jīng)寫(xiě)入多少數(shù)據(jù)。
nNumberOfBytesToRead是多少數(shù)據(jù)要讀取。
nNumberOfBytesToRead是已經(jīng)讀取多少數(shù)據(jù)。
lpOverlapped是異步讀寫(xiě)的結(jié)構(gòu)。
調(diào)用函數(shù)的例子如下:
#001 //創(chuàng)建、寫(xiě)入、讀取文件。
#002 //蔡軍生 2007/10/21 QQ:9073204 深圳
#003 void CreateFileDemo(void)
#004 {
#005 //
#006 HANDLE hFile = ::CreateFile(_T("CreateFileDemo.txt"), //創(chuàng)建文件的名稱(chēng)。
#007 GENERIC_WRITE|GENERIC_READ, // 寫(xiě)和讀文件。
#008 0, // 不共享讀寫(xiě)。
#009 NULL, // 缺省安全屬性。
#010 CREATE_ALWAYS, // 如果文件存在,也創(chuàng)建。
#011 FILE_ATTRIBUTE_NORMAL, // 一般的文件。
#012 NULL); // 模板文件為空。
#013
#014 if (hFile == INVALID_HANDLE_VALUE)
#015 {
#016 //
#017 OutputDebugString(_T("CreateFile fail!\r\n"));
#018 }
#019
#020 //往文件里寫(xiě)數(shù)據(jù)。
#021 const int BUFSIZE = 4096;
#022 char chBuffer[BUFSIZE];
#023 memcpy(chBuffer,"Test",4);
#024 DWORD dwWritenSize = 0;
#025 BOOL bRet = ::WriteFile(hFile,chBuffer,4,&dwWritenSize,NULL);
#026 if (bRet)
#027 {
#028 //
#029 OutputDebugString(_T("WriteFile 寫(xiě)文件成功\r\n"));
#030 }
#031
#032 //先把寫(xiě)文件緩沖區(qū)的數(shù)據(jù)強(qiáng)制寫(xiě)入磁盤(pán)。
#033 FlushFileBuffers(hFile);
#034
#035 //
#036 //從文件里讀取數(shù)據(jù)。
#037 LONG lDistance = 0;
#038 DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
#039 if (dwPtr == INVALID_SET_FILE_POINTER)
#040 {
#041 //獲取出錯(cuò)碼。
#042 DWORD dwError = GetLastError() ;
#043 //處理出錯(cuò)。
#044 }
#045
#046 DWORD dwReadSize = 0;
#047 bRet = ::ReadFile(hFile,chBuffer,4,&dwReadSize,NULL);
#048 if (bRet)
#049 {
#050 //
#051 OutputDebugString(_T("ReadFile 讀文件成功\r\n"));
#052 }
#053 else
#054 {
#055 //獲取出錯(cuò)碼。
#056 DWORD dwError = GetLastError();
#057 //處理出錯(cuò)。
#058 TCHAR chErrorBuf[1024];
#059 wsprintf(chErrorBuf,_T("GetLastError()=%d\r\n"),dwError);
#060 OutputDebugString(chErrorBuf);
#061 }
#062
#063 }
SetEndOfFile和GetFileSizeEx函數(shù)
有一天,我正在開(kāi)發(fā)BT軟件,它有這樣的一個(gè)功能,就是先把文件的大小分配好,然后再慢慢地往里面對(duì)應(yīng)的位置寫(xiě)入相應(yīng)的數(shù)據(jù)。這樣的好處,就是可以先把磁盤(pán)空間占用起來(lái),以便后面的下載順利進(jìn)行。要實(shí)現(xiàn)這個(gè)功能,就需要?jiǎng)?chuàng)建一個(gè)空的文件,然后把文件指針設(shè)置到相應(yīng)大小的位置,然后再調(diào)用函數(shù)SetEndOfFile來(lái)設(shè)置文件的結(jié)束位置,這樣文件就有相應(yīng)的大小了。在BT軟件的開(kāi)發(fā)里,也發(fā)現(xiàn)目錄的處理時(shí)需要詳細(xì)地記錄目錄里的文件大小,這就需要使用GetFileSizeEx函數(shù)來(lái)獲取文件的大小。由于BT里的視頻文件比較大,有可能幾G的,一定要使用GetFileSizeEx函數(shù)來(lái)處理,這樣就可以獲取比較大的文件而不出錯(cuò)。
函數(shù)FlushFileBuffers和SetFilePointer聲明如下:
WINBASEAPI
BOOL
WINAPI
SetEndOfFile(
__in HANDLE hFile
);
BOOL
WINAPI
GetFileSizeEx(
__in HANDLE hFile,
__out PLARGE_INTEGER lpFileSize
);
hFile是文件句柄。
lpFileSize是獲取文件返回的大小。
調(diào)用函數(shù)的例子如下:
#001 //創(chuàng)建、寫(xiě)入、讀取文件。
#002 //蔡軍生 2007/10/23 QQ:9073204 深圳
#003 void CreateFileDemo(void)
#004 {
#005 //
#006 HANDLE hFile = ::CreateFile(_T("CreateFileDemo.txt"), //創(chuàng)建文件的名稱(chēng)。
#007 GENERIC_WRITE|GENERIC_READ, // 寫(xiě)和讀文件。
#008 0, // 不共享讀寫(xiě)。
#009 NULL, // 缺省安全屬性。
#010 CREATE_ALWAYS, // 如果文件存在,也創(chuàng)建。
#011 FILE_ATTRIBUTE_NORMAL, // 一般的文件。
#012 NULL); // 模板文件為空。
#013
#014 if (hFile == INVALID_HANDLE_VALUE)
#015 {
#016 //
#017 OutputDebugString(_T("CreateFile fail!\r\n"));
#018 }
#019
#020 //往文件里寫(xiě)數(shù)據(jù)。
#021 const int BUFSIZE = 4096;
#022 char chBuffer[BUFSIZE];
#023 memcpy(chBuffer,"Test",4);
#024 DWORD dwWritenSize = 0;
#025 BOOL bRet = ::WriteFile(hFile,chBuffer,4,&dwWritenSize,NULL);
#026 if (bRet)
#027 {
#028 //
#029 OutputDebugString(_T("WriteFile 寫(xiě)文件成功\r\n"));
#030 }
#031
#032 //先把寫(xiě)文件緩沖區(qū)的數(shù)據(jù)強(qiáng)制寫(xiě)入磁盤(pán)。
#033 FlushFileBuffers(hFile);
#034
#035 //
#036 //從文件里讀取數(shù)據(jù)。
#037 LONG lDistance = 0;
#038 DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
#039 if (dwPtr == INVALID_SET_FILE_POINTER)
#040 {
#041 //獲取出錯(cuò)碼。
#042 DWORD dwError = GetLastError() ;
#043 //處理出錯(cuò)。
#044 }
#045
#046 DWORD dwReadSize = 0;
#047 bRet = ::ReadFile(hFile,chBuffer,4,&dwReadSize,NULL);
#048 if (bRet)
#049 {
#050 //
#051 OutputDebugString(_T("ReadFile 讀文件成功\r\n"));
#052 }
#053 else
#054 {
#055 //獲取出錯(cuò)碼。
#056 DWORD dwError = GetLastError();
#057 //處理出錯(cuò)。
#058 TCHAR chErrorBuf[1024];
#059 wsprintf(chErrorBuf,_T("GetLastError()=%d\r\n"),dwError);
#060 OutputDebugString(chErrorBuf);
#061 }
#062
#063 //
#064 //
#065 //移動(dòng)文件指針到新的位置。
#066 lDistance = 3;
#067 dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
#068
#069 //設(shè)置文件新的結(jié)束位置。
#070 ::SetEndOfFile(hFile);
#071
#072 //獲取文件的大小。
#073 LARGE_INTEGER liFileSize;
#074 ::GetFileSizeEx(hFile,&liFileSize);
#075
#076 TCHAR chTemp[128];
#077 wsprintf(chTemp,_T("GetFileSizeEx()=%d\r\n"),liFileSize);
#078 OutputDebugString(chTemp);
#079
#080
#081 //關(guān)閉文件。
#082 if (hFile != INVALID_HANDLE_VALUE)
#083 {
#084 //
#085 CloseHandle(hFile);
#086 }
#087
#088 }
fseek()函數(shù)
懸賞分:0 | 解決時(shí)間:2007-1-6 10:52 | 提問(wèn)者:
409058355 |
檢舉int fseek( FILE *stream, long offset, int origin ); 第一個(gè)參數(shù)stream為文件指針,offset為偏移,比如你要從文件的第10000個(gè)字節(jié)開(kāi)始讀取的話,offset就應(yīng)該為10000,origin 為標(biāo)志是從文件開(kāi)始還是末尾。 origin 的取值: SEEK_CUR Current position of file pointer SEEK_END End of file SEEK_SET Beginning of file 那么fseek(fp,-size,1)中-size和1是否應(yīng)理解為,從文件倒說(shuō)第一個(gè)文件開(kāi)始讀取,可是感覺(jué)好別扭,誰(shuí)能告訴我啊 ~~謝謝
最佳答案
fseek最后一個(gè)參數(shù)最好不要直接指定一個(gè)數(shù)值,比如1, 要使用SEEK_CUR,SEEK_END,SEEK_SET 第二個(gè)參數(shù)表示相對(duì)于第三個(gè)參數(shù)的偏移,整數(shù)表示正向偏移,負(fù)數(shù)表示負(fù)向偏移,比如 fseek(fp,-size,SEEK_CUR); 從當(dāng)前位置向文件后方(比如文件有123三個(gè)數(shù)字,那么2在三的后方,3在2的前方) fseek(fp,size,SEEK_SET); 從文件開(kāi)始位置向前移動(dòng)size 這里默認(rèn)size是正數(shù)
41
CreateFile函數(shù)詳解CreateFile
The CreateFile function creates or opens the following objects and returns a handle that can be used to access
the object:
files
pipes
mailslots
communications resources
disk devices(Windows NT only)
consoles
directories(open only)
CreateFile 函數(shù)創(chuàng)建或打開(kāi)下列對(duì)象,并返回一個(gè)可以用來(lái)訪問(wèn)這些對(duì)象的句柄。
文件
pipes
郵槽
通信資源
磁盤(pán)驅(qū)動(dòng)器(僅適用于 windowsNT )
控制臺(tái)
文件夾(僅用于打開(kāi))
HANDLE CreateFile(
LPCTSTR lpFileName, // 指向文件名的指針
DWORD dwDesiredAccess, // 訪問(wèn)模式(寫(xiě) / 讀)
DWORD dwShareMode, // 共享模式
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // 指向安全屬性的指針
DWORD dwCreationDisposition, // 如何創(chuàng)建
DWORD dwFlagsAndAttributes, // 文件屬性
HANDLE hTemplateFile // 用于復(fù)制文件句柄
);
Parametes
參數(shù)列表
參數(shù)
類(lèi)型及說(shuō)明
lpFileName
String ,要打開(kāi)的文件的名字
dwDesiredAccess
Long ,如果為 GENERIC_READ 表示允許對(duì)設(shè)備進(jìn)行讀訪問(wèn);如果為GENERIC_WRITE 表示允許對(duì)設(shè)備進(jìn)行寫(xiě)訪問(wèn)(可組合使用);如果為零,表示只允許獲取與一個(gè)設(shè)備有關(guān)的信息
dwShareMode
Long ,零表示不共享; FILE_SHARE_READ 和 / 或FILE_SHARE_WRITE 表示允許對(duì)文件進(jìn)行共享訪問(wèn)
lpSecurityAttributes
SECURITY_ATTRIBUTES ,指向一個(gè) SECURITY_ATTRIBUTES 結(jié)構(gòu)的指針,定義了文件的安全特性(如果操作系統(tǒng)支持的話)
dwCreationDisposition
Long ,下述常數(shù)之一:
CREATE_NEW
創(chuàng)建文件;如文件存在則會(huì)出錯(cuò)
CREATE_ALWAYS
創(chuàng)建文件,會(huì)改寫(xiě)前一個(gè)文件
OPEN_EXISTING
文件必須已經(jīng)存在。由設(shè)備提出要求
OPEN_ALWAYS
如文件不存在則創(chuàng)建它
TRUNCATE_EXISTING
講現(xiàn)有文件縮短為零長(zhǎng)度
dwFlagsAndAttributes
Long ,一個(gè)或多個(gè)下述常數(shù)
FILE_ATTRIBUTE_ARCHIVE
標(biāo)記歸檔屬性
FILE_ATTRIBUTE_COMPRESSED
將文件標(biāo)記為已壓縮,或者標(biāo)記為文件在目錄中的默認(rèn)壓縮方式
FILE_ATTRIBUTE_NORMAL
默認(rèn)屬性
FILE_ATTRIBUTE_HIDDEN
隱藏文件或目錄
FILE_ATTRIBUTE_READONLY
文件為只讀
FILE_ATTRIBUTE_SYSTEM
文件為系統(tǒng)文件
FILE_FLAG_WRITE_THROUGH
操作系統(tǒng)不得推遲對(duì)文件的寫(xiě)操作
FILE_FLAG_OVERLAPPED
允許對(duì)文件進(jìn)行重疊操作
FILE_FLAG_NO_BUFFERING
禁止對(duì)文件進(jìn)行緩沖處理。文件只能寫(xiě)入磁盤(pán)卷的扇區(qū)塊
FILE_FLAG_RANDOM_ACCESS
針對(duì)隨機(jī)訪問(wèn)對(duì)文件緩沖進(jìn)行優(yōu)化
FILE_FLAG_SEQUENTIAL_SCAN
針對(duì)連續(xù)訪問(wèn)對(duì)文件緩沖進(jìn)行優(yōu)化
FILE_FLAG_DELETE_ON_CLOSE
關(guān)閉了上一次打開(kāi)的句柄后,將文件刪除。特別適合臨時(shí)文件
也可在 Windows NT 下組合使用下述常數(shù)標(biāo)記:
SECURITY_ANONYMOUS , SECURITY_IDENTIFICATION ,SECURITY_IMPERSONATION , SECURITY_DELEGATION ,SECURITY_CONTEXT_TRACKING ,SECURITY_EFFECTIVE_ONLY
hTemplateFile
Long ,如果不為零,則指定一個(gè)文件句柄。新文件將從這個(gè)文件中復(fù)制擴(kuò)展屬性
返回值
如執(zhí)行成功,則返回文件句柄。 INVALID_HANDLE_VALUE 表示出錯(cuò),會(huì)設(shè)置
GetLastError 。即使函數(shù)成功,但若文件存在,且指定了 CREATE_ALWAYS 或 OPEN_ALWAYS ,
GetLastError 也會(huì)設(shè)為ERROR_ALREADY_EXISTS
lpFileName
Pointer to a null-terminated string that specifies the name of the object(file, pipe, mailslot,
communications resource, disk device, console, or directory) to create or open.
指向一個(gè)空結(jié)尾字符串。該參數(shù)指定了用于創(chuàng)建或打開(kāi)句柄的對(duì)象。
if *lpFileName is a path, there is a default string size limit of MAX_PATH characters, This limit is
related to how the CreateFile function parses paths.
如果 lpFileName 的對(duì)象是一個(gè)路徑,則有一個(gè)最大字符數(shù)的限制。不能超過(guò)常量 (MAX_PATH). 這個(gè)限制指示了
CreateFile 函數(shù)如何解析路徑 .
dwDesiredAccess
Specifies the type of access to the object. An application can obtain read access, write access,
read-write access, or device query access, This parameter can be any combination of the following
values
指定對(duì)象的訪問(wèn)方式 , 程序可以獲得讀訪問(wèn)權(quán) , 寫(xiě)訪問(wèn)權(quán) , 讀寫(xiě)訪問(wèn)權(quán)或者是詢(xún)問(wèn)設(shè)備 ("device query")訪問(wèn)權(quán) .
這個(gè)參數(shù)可以是下列值的任意組合
Value( 值 ) Meaning( 含義 )
0 Specifies device query access to the object. An application can query device
attributes without accessing the device.
指定詢(xún)問(wèn)訪問(wèn)權(quán) . 程序可以在不直接訪問(wèn)設(shè)備的情況下查詢(xún)?cè)O(shè)備的屬性 .
GENERIC_READ Specifies read access to the object, Data can be read from the file and the
file pointer can be moved. Combine with GENERIC_WRITE for read-write access.
指定讀訪問(wèn)權(quán) . 可以從文件中讀取數(shù)據(jù) , 并且移動(dòng)文件指針 . 可以和 GENERIC_WRITE 組合
成為 " 讀寫(xiě)訪問(wèn)權(quán) ".
GENERIC_WRITE specifies write access to the object. Data can be written to the file and the
file pointer can be moved. Combine with GENERIC_READ fro read-write access
指定寫(xiě)訪問(wèn)權(quán) . 可以從文件中寫(xiě)入數(shù)據(jù) , 并且移動(dòng)文件指針 . 可以和 GENERIC_READ 組合
成為 " 讀寫(xiě)訪問(wèn)權(quán) ".
dwShareMode
Set of bit flags that specifies how the object can be shared, If dwShareMode is 0, the object cannot
be shared. Subsequent open operations on the object will fail, until the handle is closed.
設(shè)置位標(biāo)志指明對(duì)象如休共享 . 如果參數(shù)是 0, 對(duì)象不能夠共享 . 后續(xù)的打開(kāi)對(duì)象的操作將會(huì)失敗 , 直到該對(duì)象的句
柄關(guān)閉 .
To share the object, use a combination of one or more of the following values:
使用一個(gè)或多個(gè)下列值的組合來(lái)共享一個(gè)對(duì)象 .
Value( 值 ) Meaning( 含義 )
FILE_SHARE_DELETE WindowsNT: Subsequent open operations on the object will succeed only if
delete access is requested.
WINDOWS NT: 后續(xù)的僅僅請(qǐng)求刪除訪問(wèn)權(quán)的打開(kāi)操作將會(huì)成功 .
FILE_SHARE_READ Subsequent open operations on the object will successd only if read access
is requested.
后續(xù)的僅僅請(qǐng)求讀訪問(wèn)權(quán)的打開(kāi)操作將會(huì)成功 .
FILE_SHARE_WRITE Subsequent open operations on the object will succeed only if write access
is requested.
后續(xù)的僅僅請(qǐng)求寫(xiě)訪問(wèn)權(quán)的打開(kāi)操作將會(huì)成功 .
lpSecurityAttributes
pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be
inherited by child processes, if lpSecurityAttributes is NULL, the handle cannot be inherited.
指向一個(gè) SECURITY_ATTRIBUTES 結(jié)構(gòu)的指針用于確定如何在子進(jìn)程中繼承這個(gè)句柄 . 如果這個(gè)參數(shù)是NULL,
則該句柄不可繼承 .
dwCreationDisposition
Specifies which action to take on files that exist, and which action to take when files do not exist.
For more information about this parameter, see the remarks section. This parameter must be one of the
following values
指定當(dāng)文件存在或者不存在時(shí)如何動(dòng)作。關(guān)于這個(gè)參數(shù)更多的信息,參考批注部分。這個(gè)參數(shù)必須是一個(gè)或多個(gè)
下列值。
VALUE( 值 ) Neaning( 含義 )
CREATE_NEW Creates a new file. The function fails if the specified file already exists
創(chuàng)建一個(gè)新文件 . 如果該文件已經(jīng)存在函數(shù)則會(huì)失敗 .
CREATE_ALWAYS Creates a new file. If the file exsts, the function overwrites the file and
clears the existing attributes.
創(chuàng)建一個(gè)新文件 . 如果該文件已經(jīng)存在 , 函數(shù)將覆蓋已存在的文件并清除已存在的文件屬性
OPEN_EXISTING Opens the file. The function fails if the file does not exist.
See the Remarks section for a discussion of why you should use the
OPEN_EXISTING flag if you are using the CreateFile function for devices,
including the console.
打開(kāi)一個(gè)文件 , 如果文件不存在函數(shù)將會(huì)失敗 .
如查你使用 CreateFile 函數(shù)為設(shè)備裝載控制臺(tái) . 請(qǐng)查看批注中的 " 為什么使用
OPEN_EXISTING 標(biāo)志 " 的部分 .
OPEN_ALWAYS Opens the file, if it exsts. If the file does not exist, the function creates
the file as if dwCreationDisposition were CREATE_NEW.
如果文件存在 , 打開(kāi)文件 . 如果文件不存在 , 并且參數(shù)中有 CREATE_NEW 標(biāo)志 , 則創(chuàng)建文件 .
TRUNCATE_EXISTING Opens the file. Once opened, the file is truncated so that its size is zero
bytes The calling process must open the file with at least GENERIC_WRITE access.
The function fails if the file does not exist.
打開(kāi)一個(gè)文件 , 每次打開(kāi) , 文件將被截至 0 字節(jié) . 調(diào)用進(jìn)程必須用 GENERIC_WRITE 訪問(wèn)模式打
開(kāi)文件 . 如果文件不存在則函數(shù)就會(huì)失敗 .
dwFlagsAndatributes
Specifies the file attributes and flags for the file.
為文件指定屬性和標(biāo)志位
Any combination of the following attributes is acceptable for the dwFlagsAndAttributes parameter,
except all other file attributes override FILE_ATTRIBUTE_NORMAL.
該參數(shù)可以接收下列屬性的任意組合 . 除非其它所有的文件屬性忽略 FILE_ATTRIBUTE_NORMAL.
Attribute( 屬性 ) Meaning( 標(biāo)志 )
FILE_ATTRIBUTE_ARCHIVE The ifle should be archived. Application use this attribute to mark
files for backup or removal.
文件將被存檔 , 程序使用此屬性來(lái)標(biāo)志文件去備份或移除
FILE_ATTRIBUTE_HIDDEN The file is hidden. It is not to be included in an ordinary directory
listing.
文件被隱藏 , 它不會(huì)在一般文件夾列表中被裝載 .
FILE_ATTRIBUTE_NORMAL The file has no other attributes set. This attribute is valid only if
used alone
文件沒(méi)有被設(shè)置任何屬性 .
FILE_ATTRIBUTE_OFFLINE The data of the file is not immediately available. Indicates that the
file data has been physically moved to offline storage.
文件的數(shù)據(jù)沒(méi)有被立即用到。指出正在脫機(jī)使用該文件。
FILE_ATTRIBUTE_READONLY The file is read only.Applications can read the file but cannot write
to it or delete it
這個(gè)文件只可讀取 . 程序可以讀文件 , 但不可以在上面寫(xiě)入內(nèi)容 , 也不可刪除 .
FILE_ATTRIBUTE_SYSTEM The file is part of or is used exclusively by the operation system.
文件是系統(tǒng)的一部分 , 或是系統(tǒng)專(zhuān)用的 .
FILE_ATTRIBUTE_TEMPORARY The file is being used for temporary storage. File systems attempt
to keep all of the data in memory for quicker access rather than
flushing the data back to mass storage. A temporary file should be
deleted by the application as soon as it is no longer needed.
文件被使用后,文件系統(tǒng)將努力為(文件的)所有數(shù)據(jù)的迅迅訪問(wèn)保持一塊
內(nèi)存。臨時(shí)文件應(yīng)當(dāng)在程序不用時(shí)及時(shí)刪除。
Any combination of the following flags is acceptable for the dwFlagsAndAttributes parameter.
dwFlagAndAttributes 可以接受下列標(biāo)志的任意組合。
FLAG (標(biāo)志) Meaning( 含義 )
FILE_FLAG_WRITE_THROUGH Instructs the system to write through any intermediate cache and go
directly to disk. The system can still cache write operations, but
cannot lazily flush them.
指示系統(tǒng)通過(guò)快速緩存直接寫(xiě)入磁盤(pán),
FILE_FLAG_OVERLAPPED Instructs the system to initialize the object, so that operations that
take a significant amount of time to process return ERROR_IO_PENDING.
When the operation is finished, the specified event is set to the
signaled state.
指示系統(tǒng)初始化對(duì)象 , 此操作將對(duì)進(jìn)程設(shè)置一個(gè)引用計(jì)數(shù)并返回 ERROR_IO_PENDING.
處理完成后 , 指定對(duì)象將被設(shè)置為信號(hào)狀態(tài) .
When you specify FILE_FLAG_OVERLAPPED, the file read and write functions
must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED
is specified, an application must perform overlapped parameter(pointing
to an OVERLAPPED structure)to the file read and write functions.
This flag also enables more than one operation to be performed
simultaneously with the handle(a simultaneous read and write operation,
for example).
當(dāng)你指定 FILE_FLAG_OVERLAPPED 時(shí) , 讀寫(xiě)文件的函數(shù)必須指定一個(gè) OVERLAPPED 結(jié)構(gòu) .
并且 . 當(dāng) FILE_FLAG_OVERLAPPED 被指定 , 程序必須執(zhí)行重疊參數(shù) ( 指向 OVERLAPPED
結(jié)構(gòu) ) 去進(jìn)行文件的讀寫(xiě) .
這個(gè)標(biāo)志也可以有超過(guò)一個(gè)操作去執(zhí)行 .
FILE_FLAG_NO_BUFFERING Instructs the system to open the file with no intermediate buffering or
caching.When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum
asynchronous performance, because the I/O does not rely on the synchronous
operations of the memory manager. However, some I/O operations will take
longer, because data is not being held in the cache.
指示系統(tǒng)不使用快速緩沖區(qū)或緩存,當(dāng)和 FILE_FLAG_OVERLAPPED 組合 , 該標(biāo)志給出最
大的異步操作量 , 因?yàn)?nbsp;I/O 不依賴(lài)內(nèi)存管理器的異步操作 . 然而 , 一些 I/O 操作將會(huì)運(yùn)行
得長(zhǎng)一些 , 因?yàn)閿?shù)據(jù)沒(méi)有控制在緩存中 .
An application must meet certain requirements when working with files
opened with FILE_FLAG_NO_BUFFERING:
當(dāng)使用 FILE_FLAG_NO_BUFFERING 打開(kāi)文件進(jìn)行工作時(shí) , 程序必須達(dá)到下列要求 :
File access must begin at byte offsets within the file that are
integer multiples of the volume's sector size.
文件的存取開(kāi)頭的字節(jié)偏移量必須是扇區(qū)尺寸的整倍數(shù) .
File access must be for numbers of bytes that are integer
multiples of the volume's sector size. For example, if the sector
size is 512 bytes, an application can request reads and writes of
512, 1024, or 2048 bytes, but not of 335, 981, or 7171bytes.
文件存取的字節(jié)數(shù)必須是扇區(qū)尺寸的整倍數(shù) . 例如 , 如果扇區(qū)尺寸是 512 字節(jié)
程序就可以讀或者寫(xiě) 512,1024 或者 2048 字節(jié) , 但不能夠是 335,981 或者 7171
字節(jié) .
buffer addresses for read and write operations must be sector
aligned(aligned on addresses in memory that are integer multiples
of the volume's sector size).
進(jìn)行讀和寫(xiě)操作的地址必須在扇區(qū)的對(duì)齊位置 , 在內(nèi)存中對(duì)齊的地址是扇區(qū)
尺寸的整倍數(shù) .
One way to align buffers on integer multiples of the volume sector size is
to use VirtualAlloc to allocate the buffers, It allocates memory that is
aligned on addresses that are integer multiples of the operating system's
memory page size. Because both memory page and volume sector sizes are
powers of 2, this memory is also aligned on addresses that are integer
multiples of a volume's sector size.
一個(gè)將緩沖區(qū)與扇區(qū)尺寸對(duì)齊的途徑是使用 VirtualAlloc 函數(shù) . 它分配與操作系統(tǒng)
內(nèi)存頁(yè)大小的整倍數(shù)對(duì)齊的內(nèi)存地址 . 因?yàn)閮?nèi)存頁(yè)尺寸和扇區(qū)尺寸 --2 都是它們的冪 .
這塊內(nèi)存在地址中同樣與扇區(qū)尺寸大小的整倍數(shù)對(duì)齊 .
An application can determine a volume's sector size by calling the
GetDiskFreeSpace function
程序可以通過(guò)調(diào)用 GetDiskFreeSpace 來(lái)確定扇區(qū)的尺寸 .
FILE_FLAG_RANDOM_ACCESS
Indicates that the file is accessed randomly. The system can use this as
a hint to optimize file caching.
指定文件是隨機(jī)訪問(wèn) , 這個(gè)標(biāo)志可以使系統(tǒng)優(yōu)化文件的緩沖 .
FILE_FLAG_SEQUENTIAL_SCAN
Indicates that the file is to be accessed sequentially from beginning to
end. The system can use this as a hint to optimize file caching. If an
application moves the file pointer for random access, optimum caching may
not occur; however, correct operation is still guaranteed.
指定文件將從頭到尾連續(xù)地訪問(wèn) . 這個(gè)標(biāo)志可以提示系統(tǒng)優(yōu)化文件緩沖 . 如果程序在
隨機(jī)訪問(wèn)文件中移動(dòng)文件指針 , 優(yōu)化可能不會(huì)發(fā)生 ; 然而 , 正確的操作仍然可以得到保
證
Specifying this flag can increase performance for applications that read
large files using sequential access, performance gains can be even more
noticeable for applications that read large files mostly sequentially,
but occasionally skip over small ranges of bytes.
指定這個(gè)標(biāo)志可以提高程序以順序訪問(wèn)模式讀取大文件的性能 , 性能的提高在許多
程序讀取一些大的順序文件時(shí)是異常明顯的 . 但是可能會(huì)有小范圍的字節(jié)遺漏 .
FILE_FLAG_DELETE_ON_CLOSE Indicates that the operating system is to delete the file immediately
after all of its handles have been closed, not just the handle for which
you specified FILE_FLAG_DELETE_ON_CLOSE.
指示系統(tǒng)在文件所有打開(kāi)的句柄關(guān)閉后立即刪除文件 . 不只有你可以指定FILE_FLAG_DELETE_ON_CLOSE
Subsequent open requests for the file will fail, unless FILE_SHARE_DELETE
is used.
如果沒(méi)有使用 FILE_SHARE_DELETE, 后續(xù)的打開(kāi)文件的請(qǐng)求將會(huì)失敗 .
FILE_FLAG_BACKUP_SEMANTICS WINDOWS NT:Indicates that the file is being opened or created for a backup
or restore operation.The system ensures that the calling process overrides
file security checks, provided it has the necessary privileges. The
relevant privileges are SE_BACKUP_NAME and SE_RESTORE_NAME.
WINDOWS NT: 指示系統(tǒng)為文件的打開(kāi)或創(chuàng)建執(zhí)行一個(gè)備份或恢復(fù)操作 . 系統(tǒng)保證調(diào)
用進(jìn)程忽略文件的安全選項(xiàng) , 倘若它必須有一個(gè)特權(quán) . 則相關(guān)的特權(quán)則是 SE_BACKUP_NAME
和 SE_RESTORE_NAME.
You can also set this flag to obtain a handle to a directory. A directory
handle can be passed to some Win32 functions in place of a file handle.
你也可以使用這個(gè)標(biāo)志獲得一個(gè)文件夾的句柄,一個(gè)文件夾句柄能夠象一個(gè)文件句柄
一樣傳給某些 Win32 函數(shù)。
FILE_FLAG_POSIX_SEMANTICS Indicates that the file is to be accessed according to POSIX rules. This
includes allowing multiple files with names, differing only in case, for file
systems that support such naming. Use care when using this option because
files created with this flag may not be accessible by applications written
for MS-DOS or 16-bit Windows.
指明文件符合 POSIX 標(biāo)準(zhǔn) . 這是在 MS-DOS 與 16 位 Windows 下的標(biāo)準(zhǔn) .
FILE_FLAG_OPEN_REPARSE_POINT Specifying this flag inhibits the reparse behavior of NTFS reparse points.
When the file is opened, a file handle is returned, whether the filter that
controls the reparse point is operational or not. This flag cannot be used
with the CREATE_ALWAYS flag.
指定這個(gè)標(biāo)志制約 NTFS 分區(qū)指針 . 該標(biāo)志不能夠和 CREAT_ALWAYS 一起使用 .
FILE_FLAG_OPEN_NO_RECALL Indicates that the file data is requested,but it should continue to reside in
remote storage. It should not be transported back to local storage. This flag
is intended for use by remote storage systems or the Hierarchical Storage
Management system.
指明需要文件數(shù)據(jù) , 但是將繼續(xù)從遠(yuǎn)程存儲(chǔ)器中接收 . 它不會(huì)將數(shù)據(jù)存放在本地存儲(chǔ)器中 .
這個(gè)標(biāo)志由遠(yuǎn)程存儲(chǔ)系統(tǒng)或等級(jí)存儲(chǔ)管理器系統(tǒng)使用 .
hTemplateFile
Specifies a handle with GENERIC_READ access to a template file. The template file supplies file attributes and
extended attributes for the file being created.
為 GENERIC_READ 訪問(wèn)的模式指定一個(gè)句柄到模板文件 . 模板文件在文件開(kāi)始創(chuàng)建后提供文件屬性和擴(kuò)展屬性 .
Return Values
返回值
If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before
the function call and dwCreation is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS
(even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero.
如果函數(shù)成功 , 返回一個(gè)打開(kāi)的指定文件的句柄 . 如果指定文件在函數(shù)調(diào)用前已經(jīng)存在并且 dwCreation 參數(shù)是 CREATE_ALWAYS 或者
OPEN_ALWAYS, 調(diào)用 GetLastError 就會(huì)返回 ERROR_ALREADY_EXISTS( 表示函數(shù)成功 ). 如果函數(shù)文件在調(diào)用前不存在則會(huì)返回 0.
If the function fails, the return value is INVALID_HANDLE_VALUE.To get extended error information, call GetLastError.
如果函數(shù)失敗 , 返會(huì)值會(huì)是 INVALID_HANDLE_VALUE. 更多的錯(cuò)誤信息可以調(diào)用 GetLastError 來(lái)獲得 .