Uso dei flussi

Nell'esempio riportato in questo argomento viene illustrato come usare i flussi di file system NTFS di base.

In questo esempio viene creato un file denominato "TestFile", con una dimensione di 16 byte. Tuttavia, il file ha anche un tipo di flusso ::$DATA aggiuntivo, denominato "Stream", che aggiunge altri 23 byte non segnalati dal sistema operativo. Pertanto, quando si visualizza la proprietà relativa alle dimensioni del file, vengono visualizzate solo le dimensioni del flusso ::$DATA predefinito per il file.

#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;
   }
}

Se si digita TestFile al prompt dei comandi, viene visualizzato l'output seguente:

This is TestFile

Tuttavia, se si digitano le parole Type TestFile:Stream, viene generato l'errore seguente:

"La sintassi del nome file, del nome della directory o dell'etichetta del volume non è corretta".

Per visualizzare ciò che si trova in TestFile:stream, usare uno dei comandi seguenti:

Altro < TestFile:Stream

Altro < TestFile:Stream:$DATA

Il testo visualizzato è il seguente:

This is TestFile:Stream

Flussi di file