使用 WriteFile() C++ 写入 .txt
Writing to .txt with WriteFile() C++
(使用 C++,Windows10,微软 Visual Studio2017)
您好,我是串口新手,正在学习如何使用它们打开、关闭、读取和写入
现在,我正在尝试使用 CreateFile()
和 WriteFile()
函数写入 txt 文件。
我创建了一个名为“write.txt”的 .txt 文件并将其保存在我的文档文件夹中。然后我通过“添加”-->“现有项目”将它添加到我的项目的源文件中。该文件为空。
我写了下面的程序,希望能写入文件,但是运行查看文件后,文件还是空的。该程序编译但我猜我在某处有错误。我要正确写入文件吗?
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main() {
HANDLE write;
write = CreateFile(TEXT("\write.txt"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (write == INVALID_HANDLE_VALUE) {
DWORD errorWrite = GetLastError();
printf("Error in opening file\n");
printf("Error = 0x%x\n", errorWrite);
}
else {
printf("Opening file successful\n");
}
WriteFile(write, "hello", 5, 0, 0);
CloseHandle(write);
return 0;
}
输出显示文件已成功打开,但 write.txt 文件仍为空。有什么建议吗?
提前谢谢你:D
您没有访问位于您的文档文件夹中的文本文件。您正在访问位于调用进程的当前工作目录当前指向的驱动器根目录下的文本文件。
您需要明确查询 OS 文档文件夹的路径(即通过 SHGetFolderPath()
or SHGetKnownFolderPath()
),然后将您的文件名附加到该路径。
此外,您 WriteFile()
的调用不正确。特别是lpNumberOfBytesWritten
参数错误:
lpNumberOfBytesWritten
A pointer to the variable that receives the number of bytes written when using a synchronous hFile
parameter. WriteFile
sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.
This parameter can be NULL only when the lpOverlapped
parameter is not NULL.
而且,您没有对 WriteFile()
进行任何错误处理,或者:
If the function succeeds, the return value is nonzero (TRUE).
If the function fails, or is completing asynchronously, the return value is zero (FALSE). To get extended error information, call the GetLastError
function.
尝试更像这样的东西:
#include <iostream>
#include <string>
#include <Windows.h>
#include <Shlobj.h>
#include <shlwapi.h>
using namespace std;
int main() {
TCHAR path[MAX_PATH] = {};
HRESULT hRes = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path);
if (FAILED(hRes)) {
printf("Can't get Documents folder path\nError = 0x%08x\n", hRes);
}
else {
PathAppend(path, TEXT("write.txt"));
HANDLE write = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (write == INVALID_HANDLE_VALUE) {
DWORD errorWrite = GetLastError();
printf("Can't open file\nError = %u\n", errorWrite);
}
else {
printf("File opened successful\n");
DWORD written;
if (!WriteFile(write, "hello", 5, &written, NULL)) {
DWORD errorWrite = GetLastError();
printf("Can't write to file\nError = %u\n", errorWrite);
} else {
printf("File written successful\n");
}
CloseHandle(write);
}
}
return 0;
}
(使用 C++,Windows10,微软 Visual Studio2017)
您好,我是串口新手,正在学习如何使用它们打开、关闭、读取和写入
现在,我正在尝试使用 CreateFile()
和 WriteFile()
函数写入 txt 文件。
我创建了一个名为“write.txt”的 .txt 文件并将其保存在我的文档文件夹中。然后我通过“添加”-->“现有项目”将它添加到我的项目的源文件中。该文件为空。
我写了下面的程序,希望能写入文件,但是运行查看文件后,文件还是空的。该程序编译但我猜我在某处有错误。我要正确写入文件吗?
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main() {
HANDLE write;
write = CreateFile(TEXT("\write.txt"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (write == INVALID_HANDLE_VALUE) {
DWORD errorWrite = GetLastError();
printf("Error in opening file\n");
printf("Error = 0x%x\n", errorWrite);
}
else {
printf("Opening file successful\n");
}
WriteFile(write, "hello", 5, 0, 0);
CloseHandle(write);
return 0;
}
输出显示文件已成功打开,但 write.txt 文件仍为空。有什么建议吗?
提前谢谢你:D
您没有访问位于您的文档文件夹中的文本文件。您正在访问位于调用进程的当前工作目录当前指向的驱动器根目录下的文本文件。
您需要明确查询 OS 文档文件夹的路径(即通过 SHGetFolderPath()
or SHGetKnownFolderPath()
),然后将您的文件名附加到该路径。
此外,您 WriteFile()
的调用不正确。特别是lpNumberOfBytesWritten
参数错误:
lpNumberOfBytesWritten
A pointer to the variable that receives the number of bytes written when using a synchronous
hFile
parameter.WriteFile
sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.This parameter can be NULL only when the
lpOverlapped
parameter is not NULL.
而且,您没有对 WriteFile()
进行任何错误处理,或者:
If the function succeeds, the return value is nonzero (TRUE).
If the function fails, or is completing asynchronously, the return value is zero (FALSE). To get extended error information, call the
GetLastError
function.
尝试更像这样的东西:
#include <iostream>
#include <string>
#include <Windows.h>
#include <Shlobj.h>
#include <shlwapi.h>
using namespace std;
int main() {
TCHAR path[MAX_PATH] = {};
HRESULT hRes = SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, path);
if (FAILED(hRes)) {
printf("Can't get Documents folder path\nError = 0x%08x\n", hRes);
}
else {
PathAppend(path, TEXT("write.txt"));
HANDLE write = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (write == INVALID_HANDLE_VALUE) {
DWORD errorWrite = GetLastError();
printf("Can't open file\nError = %u\n", errorWrite);
}
else {
printf("File opened successful\n");
DWORD written;
if (!WriteFile(write, "hello", 5, &written, NULL)) {
DWORD errorWrite = GetLastError();
printf("Can't write to file\nError = %u\n", errorWrite);
} else {
printf("File written successful\n");
}
CloseHandle(write);
}
}
return 0;
}