0

i am trying to open a file handler to a path i got from file, i have input file which has a full path in it for example : c:\def\es1.txt

i replaced the "\" char to double "\" so it will fit string format and then i am using :

myfile = fopen("temp.txt", "r");

while (fgets(line, line_size, myfile) != NULL){


    printf("==============================\n");
    printf(line);
    system("PAUSE\n");
    mbstowcs(wtext, line, strlen(line) + 1);//Plus null
    _tprintf(wtext);


    LPWSTR ptr = wtext;
    hFile = CreateFile(wtext,                // name of the write
        GENERIC_WRITE,          // open for writing
        0,                      // do not share
        NULL,                   // default security
        OPEN_EXISTING,             // create new file only
        FILE_ATTRIBUTE_NORMAL,  // normal file
        NULL);                  // no attr. template

    if (hFile == INVALID_HANDLE_VALUE)
    {
        DisplayError(TEXT("CreateFile"));
        _tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), wtext);
        return;

    }
    else {
        printf("yes!!!!!!!\n");
    }

when the command _tprintf(wtext); occurs i see the string as it should be: "c:\def\es1.txt"

but the CreateFile command fails:

FATAL ERROR: Unable to output error code.
ERROR: CreateFile failed with error code 123 as follows:
The filename, directory name, or volume label syntax is incorrect.
Terminal failure: Unable to open file "c:\\def\\es1.txt
" for write.

when i replace the wtext variable in CreateFile with :L"c:\\def\\es1.txt" it works fine, what is the problem?

Khaled.K
  • 5,828
  • 1
  • 33
  • 51
Omega Doe
  • 325
  • 1
  • 2
  • 17
  • 2
    Please tag this with win32, it is not a pure c question. – Segmented Mar 10 '15 at 13:40
  • @Segmented: Are you familiar with how Windows questions are properly tagged here? `w32` doesn't have a tag wiki (and only 6 questions and should probably be burninated), and I can't spot the difference between `windows` and `winapi`. (If my answer turns out to fix the problem completely, it maybe isn't a Windows question at all anymore but a basic misunderstanding about C strings.) – mafso Mar 10 '15 at 14:25
  • @mafso I suppose winapi would be the correct tag, it seems win32 is less common (as nowadays its not just 32-bit =) – Segmented Mar 10 '15 at 17:43

3 Answers3

2

Are you sure that your file which contains the path doesn't contains any special char at the end ? Like a \r or \n ?

You can print the strlen and know if your string contains only classic char.

Oneil67
  • 98
  • 8
  • Yes, the clue is seen in the output - a `newline` immediately after the file name. Can you provide a solution to remove it? – Weather Vane Mar 10 '15 at 13:40
  • Look at this : http://stackoverflow.com/questions/5457608/how-to-remove-a-character-from-a-string-in-c and remove the last or the two lasts chars of your string – Oneil67 Mar 10 '15 at 13:48
  • I meant, provide a full answer that solves the problem. Trailing character(s) might or might not be present. – Weather Vane Mar 10 '15 at 13:53
  • You can simply test if they are present or not... dumb example : `while (str[size-1] == '\n') { path_real_size--; } resize(str, path_real_size);` – Oneil67 Mar 10 '15 at 14:03
  • The error message seems quite clear, there is indeed a newline at the end of `wtext`. – mafso Mar 10 '15 at 14:37
  • The following should remove it: `wchar_t *p = wcschr(wtext, '\n'); if (p) *p = '\0';` – Segmented Mar 10 '15 at 17:41
1

I replaced the "\" char to double "\" so it will fit string format

A backslash in a string is a backslash. That they must be escaped in string literals doesn't mean they must be doubled in every string you process. In other words, "\\" is a string literal containing exactly one backslash.

A file named c:\\def\\es1.txt with double-backslashes doesn't seem to exist, so opening fails. At least that's what I'm guessing. I'm not familiar with Windows; under Linux, double-slashes in file names are interpreted as one slash.

mafso
  • 5,433
  • 2
  • 19
  • 40
0

Thank you all, it was the newline and the need to clear the char var:

while (fgets(line, line_size, myfile) != NULL){


        printf("==============================\n");
        printf(line);


        //solution
        char deststring[BUFFER];
        memset(deststring, '\0', sizeof deststring);
        strncpy(deststring, line, strlen(line) - 1);


        mbstowcs(wtext, deststring, strlen(deststring) + 1);//Plus null
        _tprintf(wtext);
Omega Doe
  • 325
  • 1
  • 2
  • 17