gpt4 book ai didi

创建文件并向其中写入内容无法按照我想要的方式工作

转载 作者:行者123 更新时间:2023-11-30 18:55:00 26 4
gpt4 key购买 nike

我编写了以下代码:

      #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他们使用几乎相同的代码。只是我不包括显示错误部分。

所以,我的问题是:它不起作用的原因可能是什么?我(程序)需要一些额外的权限才能做到这一点吗?例如,我在 Ubuntu 中使用 open() 和 write() 编写了相同的内容,只是我使用“/tmp/file.txt”作为目标目录。而且无需额外权限即可使用。

最诚挚的问候,

最佳答案

代码使用了错误的桌面目录路径。

对于 Windows 7(以及大多数版本的 Windows),路径为:

确保在每个目录级别使用两个反斜杠

C:\Users\您的用户名\桌面

关于创建文件并向其中写入内容无法按照我想要的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com