I'm working on a sequel to my File splitter, which basically does the opposite thing, stick several files together into 1 big file. This is my code so far:
Code:
//Neo1
#define _WIN32_WINNT 0x0500
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <exception>
int main(int argc, char* argv[])
{
if(argc < 3)
{
std::cerr << "Invalid number of arguments, "
<< "Correct usage is: mash OUTPUTFILE INPUTFILES" << std::endl;
return 1;
}
HANDLE hOutputFile = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if(hOutputFile == INVALID_HANDLE_VALUE)
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
std::vector <std::string> InputFiles;
DWORD i;
for(i = 3; i < argc; i++)
{
InputFiles.push_back(argv[i]);
}
for(DWORD j = 0; j < i; j++)
{
char *pBuffer = NULL;
LARGE_INTEGER li;
DWORD CurrentFileSize, dwBytesRead, lpNumberOfBytesWritten;
std::string CurrentInputFile = InputFiles.at(j);
HANDLE hInputFile = CreateFile(CurrentInputFile.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hInputFile == INVALID_HANDLE_VALUE)
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
if(!GetFileSizeEx(hInputFile, &li))
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
CurrentFileSize = li.QuadPart;
try
{
pBuffer = new char[CurrentFileSize];
}
catch (std::bad_alloc&)
{
std::cerr << "Error allocating memory." << std::endl;
return 1;
}
if(!ReadFile(hInputFile, pBuffer, CurrentFileSize, &dwBytesRead, NULL))
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
if(!WriteFile(hOutputFile, pBuffer, CurrentFileSize, &lpNumberOfBytesWritten, NULL))
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
if(lpNumberOfBytesWritten != dwBytesRead)
{
std::cerr << "A read/write occured." << std::endl;
return 1;
}
if(!CloseHandle(hInputFile))
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
delete[] pBuffer;
}
if(!CloseHandle(hOutputFile))
{
std::cerr << "Error " << GetLastError() << " occured." << std::endl;
return 1;
}
return 0;
}
This time i included error checking first time around But i'm still getting an error when running the program with valid parameters.
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
What is wrong with the program? Anyone of you geniuses that can spot the error?