0

ファイルから取得したパスへのファイル ハンドラーを開こうとしています。たとえば、c:\def\es1.txt などのフル パスを含む入力ファイルがあります。

「\」文字を2倍の「\」に置き換えたので、文字列形式に適合し、次を使用しています:

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");
    }

コマンド _tprintf(wtext); 「c:\def\es1.txt」という文字列が表示されます。

ただし、CreateFile コマンドは失敗します。

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.

CreateFile の wtext 変数を次のように置き換えると、正常L"c:\\def\\es1.txt" に動作しますが、何が問題なのですか?

4

3 に答える 3

2

パスを含むファイルの末尾に特別な文字が含まれていないことを確認してください。\r または \n のように?

strlenを出力して、文字列にクラシック char のみが含まれているかどうかを確認できます。

于 2015-03-10T13:38:48.940 に答える
1

文字列形式に合うように、「\」文字を二重の「\」に置き換えました

文字列内のバックスラッシュはバックスラッシュです。文字列リテラルでエスケープする必要があるということは、処理するすべての文字列でそれらを 2 倍にする必要があるという意味ではありません。つまり、"\\"バックスラッシュを 1 つだけ含む文字列リテラルです。

バックスラッシュが 2 つ付いた名前のファイルc:\\def\\es1.txtが存在しないようで、開くことができません。少なくともそれは私が推測していることです。私は Windows に詳しくありません。Linux では、ファイル名の二重スラッシュは 1 つのスラッシュとして解釈されます。

于 2015-03-10T14:09:35.010 に答える
0

ありがとうございました。これは改行であり、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);
于 2015-03-10T14:44:22.470 に答える