1

A very basic question, I cannot understand why I get error 2 with the simplest example of windows system programming book. I post the source:

#include <Windows.h>
#include <stdio.h>
#define BUF_SIZE 65536

int main(int argc, LPTSTR argv[]){

HANDLE fileIN, fileOUT;
DWORD nIn, nOut;        //numero di byte LETTI/SCRITTI
CHAR buffer[BUF_SIZE];

if(argc!=3){
    printf("Usage: %s file1 file2\n", argv[0]);
    return 1;
}
//printf("%s\n",argv[0]);
//printf("%s\n",argv[1]);
//printf("%s\n",argv[2]);

fileIN = CreateFileW(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(fileIN == INVALID_HANDLE_VALUE){
    printf("Invalid handle value, error %x\n", GetLastError());
    return 2;
}
fileOUT = CreateFileW(argv[2], GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
if(fileOUT == INVALID_HANDLE_VALUE){
    printf("Invalid handle value, error %x\n", GetLastError());
    return 3;
}

while(ReadFile(fileIN, buffer, BUF_SIZE, &nIn, NULL) && nIn > 0){
    WriteFile(fileOUT, buffer, nIn, &nOut, NULL);
    if(nIn != nOut){
        printf("During copy, error %x\n", GetLastError());
    return 4;
    }
}
CloseHandle(fileIN);
CloseHandle(fileOUT);
return 0;
}

Can you help me please? I'm using MS VSc++ 2010 SP 1 Thank you all in advange N.

Neurophobos
  • 11
  • 1
  • 4
  • 1
    The error value 2 means that it can not find the file ([see here](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx)). Most likely you are not providing command line arguments, which are required by the program (it uses argv[1] and argv[2] as the input and output file paths...) – qqbenq Sep 25 '15 at 16:26
  • See this [question](http://stackoverflow.com/questions/298708/debugging-with-command-line-parameters-in-visual-studio) for help on how to provide command line arguments while debugging. – qqbenq Sep 25 '15 at 16:32
  • Hi! Thank you for your answer. unfortunately i provide command line arguments, that's what's strange for me. I fear it's an unicode related issue. I've tried everything, even in debugger with arguments...nothing..maybe it's character encoding? – Neurophobos Sep 25 '15 at 17:07

1 Answers1

0

Please, forgive me if I am wrong but I believe that you are trying to create files with functions unicode and, may be, you are providing paths ansi. It is easy to test if I am wrong changing the terminating W into A. Please, forgive my possible error.