使用數據流
本主題中的範例示範如何使用基本NTFS檔案系統數據流。
此範例會建立名為 「TestFile」 的檔案,大小為 16 個字節。 不過,檔案也有額外的 ::$DATA數據流類型,名為 “Stream”,這會新增作系統未報告的額外 23 個字節。 因此,當您檢視檔案的檔案大小屬性時,只會看到檔案的預設 ::$DATA數據流大小。
#include <windows.h>
#include <stdio.h>
void main( )
{
HANDLE hFile, hStream;
DWORD dwRet;
hFile = CreateFile( TEXT("TestFile"), // Filename
GENERIC_WRITE, // Desired access
FILE_SHARE_WRITE, // Share flags
NULL, // Security Attributes
OPEN_ALWAYS, // Creation Disposition
0, // Flags and Attributes
NULL ); // OVERLAPPED pointer
if( hFile == INVALID_HANDLE_VALUE )
{
printf( "Cannot open TestFile\n" );
return;
}
else
{
WriteFile( hFile, // Handle
"This is TestFile", // Data to be written
16, // Size of data, in bytes
&dwRet, // Number of bytes written
NULL ); // OVERLAPPED pointer
CloseHandle( hFile );
hFile = INVALID_HANDLE_VALUE;
}
hStream = CreateFile( TEXT("TestFile:Stream"), // Filename
GENERIC_WRITE, // Desired access
FILE_SHARE_WRITE, // Share flags
NULL, // Security Attributes
OPEN_ALWAYS, // Creation Disposition
0, // Flags and Attributes
NULL ); // OVERLAPPED pointer
if( hStream == INVALID_HANDLE_VALUE )
printf( "Cannot open TestFile:Stream\n" );
else
{
WriteFile( hStream, // Handle
"This is TestFile:Stream", // Data to be written
23, // Size of data
&dwRet, // Number of bytes written
NULL); // OVERLAPPED pointer
CloseHandle( hStream );
hStream = INVALID_HANDLE_VALUE;
}
}
如果您在命令提示字元中輸入 Type TestFile,則會顯示下列輸出:
This is TestFile
不過,如果您輸入字組 Type TestFile:Stream,則會產生下列錯誤:
「檔名、目錄名稱或磁碟區標籤法不正確。」
若要檢視 TestFile:stream 中的內容,請使用下列其中一個命令:
更多 < TestFile:Stream
更多 < TestFile:Stream:$DATA
顯示的文字如下所示:
This is TestFile:Stream
相關主題