Novell Home

How to create a 5 GB file from a Win32 program

From Developer Community

This sample lets you create 5 GB files for testing purposes, on NTFS and NetWare NSS volumes mapped via CIFS.

Sample Code

#include <stdio.h>
#include <stdlib.h>

#ifdef __MWERKS__
#pragma ANSI_strict off
#endif
#include <windows.h>
#ifdef __MWERKS__
#pragma ANSI_strict reset
#endif

int main( int argc, char* argv[]) {
   HANDLE  handle;
   char    buffer[ 1024] = {0};
   DWORD   numberOfBytesWritten;
   __int64 i64;
   if ( argc != 2) {
      puts( "Usage: CreateLFile <filename to create>");
      return EXIT_FAILURE;
   }
   handle = CreateFile( argv[ 1], GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
   if ( handle == INVALID_HANDLE_VALUE) {
      printf( "CreateFile failed, GetLastError() = %u\n", GetLastError());
      return EXIT_FAILURE;
   }
   for ( i64 = 0; i64 < 5 * 1024 * 1024; ++i64) {
      sprintf( buffer, "%u", i64);
      if ( WriteFile( handle, buffer, sizeof( buffer), &numberOfBytesWritten, NULL) != TRUE) {
         printf( "CreateFile failed, GetLastError() = %u\n", GetLastError());
         CloseHandle( handle);
         return EXIT_FAILURE;
      }
      if ( i64 %( 1024 * 1024) == 0) {
         printf( "%u GB(s) created\n", ( unsigned int) i64 /( 1024 * 1024));
      }
   }
   printf( "%u GB(s) created\n", ( unsigned int) i64 /( 1024 * 1024));
   CloseHandle( handle);
   return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.