//A program to inject random lines between original lines of a file.
//30-JUL-2010
//include headers and libraries
#include
#include
#include
#include
#include
#include
#include
//define constants and macros
#define BUFFERSIZE 1024
//declare functions prototypes
void DisplayError(LPTSTR lpszFunction);
int fReadTheFile(LPCTSTR, char[]);
void fWriteTheFile(LPCTSTR, char[]);
void fCloseFile(HANDLE);
//
// Note: this simplified sample assumes the file to read is an ANSI text file
// only for the purposes of output to the screen. CreateFile and ReadFile
// do not use parameters to differentiate between text and binary file types.
//
//main() function definition
void __cdecl _tmain(int argc, TCHAR *argv[])
{
//variable declarations for main function
char ReadedBuffer[BUFFERSIZE] = {0};
char BuildedBuffer[BUFFERSIZE] = {0};
DWORD dwBytesReaded = 0;
DWORD dwNewContentBufferLength = 0;
if( argc != 2 )
{
printf("Usage Error: Incorrect number of arguments\n\n");
_tprintf(TEXT("Usage:\n\t%s
return;
}
dwBytesReaded = fReadTheFile(argv[1], ReadedBuffer);
dwNewContentBufferLength = fGenerateNewFileContent(ReadedBuffer, BuildedBuffer);
fWriteTheFile(argv[1], BuildedBuffer);
printf("ReadedBuffer:\n%s\n\nBuildedBuffer:\n%s\n\n", ReadedBuffer, BuildedBuffer);
}
//fReadTheFile() function definition
int fReadTheFile(LPCTSTR fileName, char ToBuffer[])
{
//variable declarations for READ section
HANDLE hFileRead;
DWORD dwBytesRead = 0;
char ReadBuffer[BUFFERSIZE] = {0};
hFileRead = CreateFile(fileName, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFileRead == INVALID_HANDLE_VALUE)
{
DisplayError(TEXT("CreateFile"));
_tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), fileName);
return -1;
}
// Read one character less than the buffer size to save room for
// the terminating NULL character.
if( FALSE == ReadFile(hFileRead, ReadBuffer, BUFFERSIZE-1, &dwBytesRead, NULL) )
{
DisplayError(TEXT("ReadFile"));
printf("Terminal failure: Unable to read from file.\n");
CloseHandle(hFileRead);
return -1;
}
// This is the section of code that assumes the file is ANSI text.
// Modify this block for other data types if needed.
if (dwBytesRead > 0 || dwBytesRead <= BUFFERSIZE)
{
ReadBuffer[dwBytesRead]='\0'; // NULL character
_tprintf(TEXT("Data read from %s (%d bytes): \n"), fileName, dwBytesRead);
printf("%s\n", ReadBuffer);
strcpy(ToBuffer, ReadBuffer);
}
else if (dwBytesRead == 0)
{
_tprintf(TEXT("No data read from file %s\n"), fileName);
}
else
{
printf("\n ** Unexpected value for dwBytesRead ** \n");
}
// It is always good practice to close the open file handles even though
// the app will exit here and clean up open handles anyway.
fCloseFile(hFileRead);
return dwBytesRead;
}
//fWriteTheFile() function definition
void fWriteTheFile(LPCTSTR fileName, char ContentBuffer[])
{
//variable declarations for WRITE section
HANDLE hFileWrite;
char DataBuffer[] = "This is some test data to write to the file.";
DWORD dwBytesToWrite = (DWORD)strlen(ContentBuffer);
DWORD dwBytesWritten = 0;
BOOL bErrorFlag = FALSE;
hFileWrite = CreateFile(fileName, // name of the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
//CREATE_NEW, // create new file only
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFileWrite == INVALID_HANDLE_VALUE)
{
DisplayError(TEXT("CreateFile"));
_tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), fileName);
return;
}
_tprintf(TEXT("Writing %d bytes to %s.\n"), dwBytesToWrite, fileName);
bErrorFlag = WriteFile(
hFileWrite, // open file handle
ContentBuffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
if (FALSE == bErrorFlag)
{
DisplayError(TEXT("WriteFile"));
printf("Terminal failure: Unable to write to file.\n");
}
else
{
if (dwBytesWritten != dwBytesToWrite)
{
// This is an error because a synchronous write that results in
// success (WriteFile returns TRUE) should write all data as
// requested. This would not necessarily be the case for
// asynchronous writes.
printf("Error: dwBytesWritten != dwBytesToWrite\n");
}
else
{
_tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, fileName);
}
}
fCloseFile(hFileWrite);
}
//fGenerateNewFileContent() function definition
int fGenerateNewFileContent(char OriginalBuffer[], char DestinationBuffer[])
{
//variable declarations for RANDOM functionality
int i, j, k=0;
char BuildBuffer[BUFFERSIZE] = {0};
char RandomBuffer[BUFFERSIZE] = {0};
DWORD dwBuildBufferLength = 0;
DWORD dwOriginalBufferLength = strlen(OriginalBuffer);
//initialize random seed:
srand ( time(NULL) );
for (i=0; i
{
BuildBuffer[k] = OriginalBuffer[i];
if (OriginalBuffer[i] == '\n')
{
for (j=0; j<40; j++)
{
RandomBuffer[j] = rand()%92 + 33;
k++;
}
RandomBuffer[39] = '\n';
RandomBuffer[40] = '\0';
strcat(BuildBuffer, RandomBuffer);
}
k++;
}
strcpy(DestinationBuffer, BuildBuffer);
dwBuildBufferLength = (DWORD)strlen(DestinationBuffer);
return dwBuildBufferLength;
}
//fCloseFile() function definition
void fCloseFile(HANDLE hFile)
{
int r;
r = CloseHandle(hFile);
if (r == 0)
{
DisplayError(TEXT("CloseHandle"));
printf("Terminal failure: Unable to close file.\n");
return;
}
return;
}
//DisplayError() function definition
void DisplayError(LPTSTR lpszFunction)
// Routine Description:
// Retrieve and output the system error message for the last-error code
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL );
lpDisplayBuf =
(LPVOID)LocalAlloc( LMEM_ZEROINIT,
( lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)lpszFunction)
+ 40) // account for format string
* sizeof(TCHAR) );
if (FAILED( StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error code %d as follows:\n%s"),
lpszFunction,
dw,
lpMsgBuf)))
{
printf("FATAL ERROR: Unable to output error code.\n");
}
_tprintf(TEXT("ERROR: %s\n"), (LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
//EOS
No comments:
Post a Comment