Thread: CreateFile Error 2 - Simple Problem, Can't Figure it out

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17

    Unhappy CreateFile Error 2 - Simple Problem, Can't Figure it out

    Hello all,

    So I haven't programmed in a long time now and I'm working through a Windows System Programming book and for some reason I can't figure out why this simple file copy program keeps giving me Error 2 (Can't find file) when the file is clearly right there. I tried searching but couldn't find someone with the same issue.

    Below is the code and a screenshot of the Command Prompt.

    Thanks!

    Code:
    #include <Windows.h>
    #include <stdio.h>
    
    #define BUF_SIZE	256
    
    int main(int argc, LPTSTR argv[])
    {
    	HANDLE hIn, hOut;
    	DWORD nIn, nOut;
    	CHAR buffer[BUF_SIZE];
    
    	if(argc != 3)
    	{
    		printf("Usage: WinFileCopy file1 file2\n");
    		return 1;
    	}
    
    	printf("Filename: %s\n", argv[1]);
    
    	hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    	if(hIn == INVALID_HANDLE_VALUE)
    	{
    		printf("Cannot open input file. Error: %x\n", GetLastError());
    		return 2;
    	}
    
    	hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    	if(hOut == INVALID_HANDLE_VALUE)
    	{
    		printf("Cannot open output file.  Error: %x\n", GetLastError());
    		return 3;
    	}
    
    	while(ReadFile(hIn, buffer, BUF_SIZE, &nIn, NULL) && nIn > 0)
    	{
    		WriteFile(hOut, buffer, nIn, &nOut, NULL);
    		if(nIn != nOut)
    		{
    			printf("Fatal write error: %x\n", GetLastError());
    			return 4;
    		}
    	}
    
    	CloseHandle(hIn);
    	CloseHandle(hOut);
    	
    	return 0;
    }
    CreateFile Error 2 - Simple Problem, Can't Figure it out-cerror-png

    Also, I tried the relative path (in above screen shot), absolute path, running as admin, etc.
    Last edited by DougD720; 04-08-2013 at 02:23 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Not usre but since no comments so far....lets start at the begining...

    Also note %x is for HEX values, GetLastError() is decimal so should be %d.

    If you put the Test.txt file in the same folder as the inFileCopy.exe (be careful if running in the debugger as it may be the 'project' folder not the 'project\Debug' folder).

    Run the exe with;

    infilecopy .\\Test.txt .\\Test2.txt

    What happens?

    NOTE:
    .\ means 'look in the working or current folder'

    Windows uses '\' as a special character (ie \n is new line) so you need 2 slashes to get one in the output (ie \\ becomes \)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Thanks for the suggestion, I had tried putting files in the project folder and the .\\ directives as well to no avail but I just ran it now and, for some reason, it's working - sort of.

    It doesn't give me an error anymore, and asks if the program can have permission to make changes to the HDD; which I click yes to. Problem is I can't find the duplicate file anywhere. I've checked all the directories and run a computer wide search for the new file but can't find it. What's weird is I built it with the release specification and that one gives the same error as before (can't find file) but the Debug version finds the file, it's just not copying it correctly. This is bizarre.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    I would check your project options.

    If you insist on using Unicode, look up
    the use of wmain function.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by RotateRight View Post
    I would check your project options.

    If you insist on using Unicode, look up
    the use of wmain function.
    Not a very helpful first post...

    Why do you think he is using UNICODE?

    I do not see a define for it before the Windows.h include.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Doug,

    If you are still trying to get this to work properly,
    how about attaching your project files and upload
    them here. I see that novacain was unable to
    resolve your problem, and I will happy to take
    a look. I suspect WideChar / Unicode issues.

    Mark

  7. #7
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Thanks guys I'd appreciate it. Seems the files are too large to upload here but here's the zipped project folder and all contents from VS 2010:

    Download WinFileCopy.zip

    Thanks!

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Doug,

    Try this (guess I need to get a newer compiler).

    Replace the line

    int main(int argc, LPTSTR argv[])

    with

    int wmain(int argc, wchar_t **argv)

    The reason:

    Since your project file has <CharacterSet>Unicode</CharacterSet>,
    CreateFileW is used to open a file. The compiler doesn't complain
    or warn you since you have defined argv to be LPTSTR.

    Using

    int main(int argc, char *argv[]), should produce warning(s).

    By using wmain, the argv parameter will match the filename in
    CreateFileW and will be able to find the files.

    Let us know!

    Mark

  9. #9
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Thanks Mark!

    That did the trick. I'll just have to change that from now on when working from the code in the book I'm using.

    Thanks again,

    Doug

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple error i can't seem to figure out
    By Shinzu911 in forum C Programming
    Replies: 2
    Last Post: 05-04-2012, 04:35 AM
  2. Replies: 7
    Last Post: 11-18-2011, 11:14 PM
  3. Replies: 19
    Last Post: 09-28-2009, 01:45 AM
  4. C++ simple problem that I can't figure out
    By dvessey in forum C++ Programming
    Replies: 9
    Last Post: 05-25-2008, 10:56 AM
  5. I simple little problem but i cant figure it out
    By SebastionV3 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2006, 05:55 PM