使用流
本主题中的示例演示如何使用基本的 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
相关主题