Windows Specific File I/O Part II

This is a discussion on Windows Specific File I/O Part II within the Windows Programming forums, part of the Platform Specific Boards category; I'm working on a sequel to my File splitter , which basically does the opposite thing, stick several files together ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Windows Specific File I/O Part II

    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?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Not positive this is it, but you're reading the file and getting the number of bytes read, but not using that value in the WriteFile call. You should have a smaller static buffer of like 8K and loop your reads and writes through this until the read returns 0 bytes.

    Code:
    char buffer[8192] = { 0 };
    while(ReadFile(hInputFile, buffer, sizeof(buffer), &dwBytesRead, NULL))
    {
        if (dwBytesRead == 0)
            break;
        if (!WriteFile(hOutputFile, buffer, dwBytesRead, &lpNumberOfBytesWritten, NULL) ||
            lpNumberOfBytesWritten != dwBytesRead)
        {
            // handle error
        }
    }
    Is there any reason you can't run this in the debugger? That should point out where exactly the error occurs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21