📄 main.cpp
字号:
#include <windows.h>
#include <stdio.h>
char acError[256];
DWORD dwLen;
DWORD dwRetVal;
int ExpandFile( char *pcFile, char *pcSize )
{
HANDLE hFile;
_ULARGE_INTEGER li;
ULARGE_INTEGER stFileSize;
BOOL bRetVal;
DWORD dwBytesWritten;
/* open the file in question */
hFile = CreateFile( pcFile, GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
printf( "error opening %s\n", pcFile );
return -1;
}
/* set up the offset to write to */
li.QuadPart = _atoi64( pcSize ) - 1;
/* if the offset to write to is less than the file size then don't do anything */
stFileSize.LowPart = GetFileSize( hFile, &stFileSize.HighPart );
if ( ( stFileSize.LowPart == -1 ) && ( GetLastError() != NO_ERROR ) )
{
dwRetVal = GetLastError();
printf( "error %d getting original file size\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
if ( li.QuadPart < stFileSize.QuadPart )
{
printf( "requested size: %I64u\n", li.QuadPart );
printf( "actual size : %I64u\n", stFileSize.QuadPart );
printf( "can't shrink file\n" ); /* could, but this util can't ;)
*/
CloseHandle( hFile );
return -1;
}
/* seek to this point in the file */
dwRetVal = SetFilePointer( hFile, li.LowPart, PLONG(&li.HighPart),FILE_BEGIN );
if ( ( dwRetVal == -1 ) && ( GetLastError() != NO_ERROR ) )
{
dwRetVal = GetLastError();
printf( "seek error %d\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
bRetVal = WriteFile( hFile, "j", 1, &dwBytesWritten, NULL );
if ( !bRetVal )
{
dwRetVal = GetLastError();
printf( "error %d writing to file\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
/* close the file */
CloseHandle( hFile );
return 0;
} /* ExpandFile */
int main( int argc, char *argv[] )
{
int i;
ULARGE_INTEGER liOffset;
HANDLE hFile;
DWORD dwBytesWritten;
BOOL bRetVal;
char acBuffer[369];
ULARGE_INTEGER liFreeBytesForCaller;
ULARGE_INTEGER liTotalBytes;
ULARGE_INTEGER liTotalFreeBytes;
char acPath[256];
char acPathAndFile[256];
if ( argc < 4 )
{
printf( "usage: ms_recreation <path> <filename> <newsize>\n" );
return -1;
}
strcpy( acPath, argv[1] );
strcpy( acPathAndFile, argv[1] );
if ( acPathAndFile[strlen(acPathAndFile)-1] != '\\' )
strcat( acPathAndFile, "\\" );
strcat( acPathAndFile, argv[2] );
/* Print some disk info */
if ( GetDiskFreeSpaceEx( acPath, &liFreeBytesForCaller,
&liTotalBytes, &liTotalFreeBytes ) )
{
printf( "Free Bytes : %I64u\n",
liFreeBytesForCaller.QuadPart );
printf( "Total Bytes : %I64u\n", liTotalBytes.QuadPart );
printf( "Total Free Bytes : %I64u\n", liTotalFreeBytes.QuadPart
);
}
if ( ExpandFile( acPathAndFile, argv[3] ) < 0 )
{
printf( "error expanding file\n" );
return -1;
}
hFile = CreateFile( acPathAndFile, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
dwRetVal = GetLastError();
printf( "error %d opening %s", dwRetVal, acPathAndFile );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
return -1;
}
/* now append some data */
printf( "appending 1000 blocks...\n" );
for ( i = 0; i < 1000; i++ )
{
liOffset.LowPart = GetFileSize( hFile, &liOffset.HighPart );
if ( ( liOffset.LowPart == -1 ) && ( GetLastError() != NO_ERROR )
)
{
dwRetVal = GetLastError();
printf( "error %d getting original file size\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
/* seek to this point in the file */
dwRetVal = SetFilePointer( hFile, liOffset.LowPart,PLONG(&liOffset.HighPart), FILE_BEGIN );
if ( ( dwRetVal == -1 ) && ( GetLastError() != NO_ERROR ) )
{
dwRetVal = GetLastError();
printf( "seek error %d\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
memset( acBuffer, 'j', 369 );
bRetVal = WriteFile( hFile, acBuffer, 369, &dwBytesWritten, NULL
);
if ( !bRetVal )
{
dwRetVal = GetLastError();
printf( "error %d writing to file\n", dwRetVal );
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRetVal,0, acError, 256, NULL ); \
acError[dwLen] = 0;
printf( "%s\n", acError );
CloseHandle( hFile );
return -1;
}
} // for
printf( "done.\n" );
CloseHandle( hFile );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -