-1

次のコードを書きます。

      #include <windows.h>
      #include "stdbool.h"
      #include <winuser.h>
      #include <WinDef.h>
      #include <Winerror.h>
      #include <stdio.h>
      #include <Strsafe.h>

      #define MAX_SIZE 100


         void DisplayError(LPTSTR lpszFunction) 
         // Routine Description:
        // Retrieve and output the system error message for the   last-error code
        { 
          LPVOID lpMsgBuf;
          LPVOID lpDisplayBuf;
          DWORD dw = GetLastError(); 

         FormatMessage(
                    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL,
                    dw,
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                    (LPTSTR) &lpMsgBuf,
                     0, 
                     NULL );

            lpDisplayBuf = 
               (LPVOID)LocalAlloc( LMEM_ZEROINIT, 
                        ( lstrlen((LPCTSTR)lpMsgBuf)
                          + lstrlen((LPCTSTR)lpszFunction)
                          + 40) // account for format string
                        * sizeof(TCHAR) );

if (FAILED( StringCchPrintf((LPTSTR)lpDisplayBuf, 
                 LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                 TEXT("%s failed with error code %d as follows:\n%s"), 
                 lpszFunction, 
                 dw, 
                 lpMsgBuf)))
{
    printf("FATAL ERROR: Unable to output error code.\n");
}

printf(TEXT("ERROR: %s\n"), (LPCTSTR)lpDisplayBuf);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);

   }


   int main(){
    //parameters of CreateFile()
     HANDLE hFile;  
     LPCTSTR lpFileName; 
     DWORD dwDesiredAccess; 
     DWORD dwShareMode;
     LPSECURITY_ATTRIBUTES lpSecurityAttributes;
     DWORD dwCreationDisposition; 
     DWORD dwFlagsAndAttributes; 
     HANDLE hTemplateFile; 

     //parameters of WriteFile()

     DWORD nNumberOfBytesToWrite;
     DWORD numberOfBytesWritten;
     LPOVERLAPPED lpOverlapped;
     char DataBuffer[MAX_SIZE];

     //others
     BOOL bErrorFlag; 

     //initialize args of CreateFile()
     lpFileName = "C:\\file.txt";
     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
     dwShareMode = 0;
     lpSecurityAttributes = NULL;
     dwCreationDisposition = CREATE_NEW;
     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; 
     hTemplateFile = NULL; 

     //initialize args of WriteFile()
     strcpy(DataBuffer, "This is the test file");
     nNumberOfBytesToWrite = (DWORD)strlen(DataBuffer);
     numberOfBytesWritten = 0;
     lpOverlapped = NULL;





   hFile = CreateFile(lpFileName, dwDesiredAccess, dwShareMode,
                    lpSecurityAttributes, dwCreationDisposition, 
                    dwFlagsAndAttributes, hTemplateFile);


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

printf(TEXT("Writing %d bytes to %s.\n"), nNumberOfBytesToWrite, lpFileName);

bErrorFlag = WriteFile(hFile, DataBuffer, nNumberOfBytesToWrite,
         &numberOfBytesWritten, lpOverlapped);
if (FALSE == bErrorFlag)
{
    DisplayError(TEXT("WriteFile"));
    printf("Terminal failure: Unable to write to file.\n");
}
else
{
    if (numberOfBytesWritten != nNumberOfBytesToWrite)
    {
        // This is an error because a synchronous write that results in
        // success (WriteFile returns TRUE) should write all data as
        // requested. This would not necessarily be the case for
        // asynchronous writes.
        printf("Error: dwBytesWritten != dwBytesToWrite\n");
    }
    else
    {
        printf(TEXT("Wrote %d bytes to %s successfully.\n"), numberOfBytesWritten, lpFileName);
    }
}

CloseHandle(hFile);
}

というわけで、ご覧の通り。デスクトップに file.txt という名前のファイルを作成し、それに短いテキストを書き込むプログラム。Microsoft Visual C++ Express を使用しており、エラーなしでコンパイルされます。しかし、緑色の再生ボタンをクリックして実行すると、デスクトップにそのようなファイルが作成されません。

考えられる障害を検索することで、https://msdn.microsoft.com/en-us/library/windows/desktop/bb540534%28v=vs.85%29.aspxで、ほぼ同じコードを使用していることも確認しました。ただし、表示エラー部分は含めておりません。

だから、私の質問: それが機能しない理由は何ですか? 私(プログラム)はそれを行うために追加の権限が必要ですか?たとえば、「/tmp/file.txt」を宛先ディレクトリとして使用することを除いて、open() & write() を使用して Ubuntu で同じことを書きました。また、追加の権限なしで機能します。

よろしくお願いします、

4

2 に答える 2

1

コードは、デスクトップ ディレクトリへの間違ったパスを使用しています。

Windows 7 (およびほとんどのバージョンの Windows) の場合、パスは次のようになります。

各ディレクトリ レベルで必ず 2 つのバックスラッシュを使用してください

C:\Users\yourUsername\Desktop

于 2015-03-14T19:06:58.773 に答える
1

これはファイル名の書き方が間違っています

"C:\Desktop\file.txt";

をエスケープする必要があります'\'。これ\fは実際にはエスケープ シーケンスです。

"C:\\Desktop\\file.txt";

また、デスクトップにファイルが作成されないようです。代わりにこれを試してください

"C:\\file.txt";

C:ドライブをチェックしてファイルを確認します。

于 2015-03-14T13:03:24.540 に答える