visual在C++中使用ReadFIle()函数时无法从文件中读取
visual Unable to Read from File while using ReadFIle() function in C++
我在使用C++的ReadFile()函数从文件中读取数据时遇到了一些问题(可能是Microsoft特有的)。
这是我的代码
写入文件
void ClientA::SharePublicKey()
{
printf("Sharing Public Keyn");
HANDLE hFile = NULL;
hFile = CreateFile(TEXT("D:\My_Proj\shared\PublicKeyB.txt"), // name of the write
GENERIC_WRITE, // open for writing
FILE_SHARE_WRITE, // do not share
NULL, // default security
CREATE_NEW, // 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"), argv[1]);
return;
}
// _tprintf(TEXT("Writing %d bytes to %s.n"), dwBytesToWrite, argv[1]);
bool bErrorFlag = WriteFile(
hFile, // open file handle
pbPublicKey, // start of data to write
dwPublicKeyLen, // number of bytes to write
&lpNumberOfBytesWritten, // number of bytes that were written
NULL); // no overlapped structure
if (FALSE == bErrorFlag)
{
// DisplayError(TEXT("WriteFile"));
printf("Terminal failure: Unable to write to file.n");
return;
}
else
{
if (lpNumberOfBytesWritten != dwPublicKeyLen)
{
// 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 != dwBytesToWriten");
}
else
{
// _tprintf(TEXT("Wrote %d bytes to %s successfully.n"), dwBytesWritten, argv[1]);
}
}
CloseHandle(hFile);
}
读取该文件
void ClientA::ReadPublicKeyOfOtherPeer()
{
HANDLE hFile = NULL;
DWORD dwBytesRead = 0;
BYTE* ReadBuffer = NULL;
OVERLAPPED ol = {0};
hFile = CreateFile(TEXT("D:\My_Proj\shared\PublicKeyB.txt"), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL // no attr. template
);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("CreateFilen"));
_tprintf(TEXT("Terminal failure: unable to open file "%s" for read.n"));
printf("Error %xn", GetLastError());
return;
}
if( FALSE == ReadFile(hFile, ReadBuffer, dwPublicKeyLen, &lpNumberOfBytesWritten, &ol) )
{
// DisplayError(TEXT("ReadFile"));
printf("Terminal failure: Unable to read from file.n GetLastError=%08xn", GetLastError());
CloseHandle(hFile);
return;
}
if (dwBytesRead > 0 && dwBytesRead <= dwPublicKeyLen-1)
{
ReadBuffer[dwBytesRead]=' '; // NULL character
//_tprintf(TEXT("Data read from %s (%d bytes): n"), argv[1], dwBytesRead);
printf("%sn", ReadBuffer);
}
else if (dwBytesRead == 0)
{
//_tprintf(TEXT("No data read from file %sn"), argv[1]);
}
else
{
// printf("n ** Unexpected value for dwBytesRead ** n");
}
retrievedPublicByteArray = ReadBuffer;
CloseHandle(hFile);
}
通过SharePublicKey方法,我将数据保存在一个文件中。我已经检查了它是否成功地将数据保存在文件上,并且文件上的数据似乎是有效的。
通过ReadPublicKeyOfOtherPeer方法,我正在读取以前保存的文件。但是阅读不成功输入输出我发现了以下行-
终端故障:无法从文件中读取。GetLastError=000003e6
您正在将未初始化的指针ReadBuffer传递给ReadFile。您需要一个足够大的缓冲区来接收结果。
相关文章:
- C++ gmock - 我们如何在单元测试 cpp 文件中读取/获取 cpp 文件函数的参数值
- 使用其他头文件函数的头文件
- 创建文件函数是否可以打开仅在WinObj实用程序中的全局目录下列出的设备的句柄?
- 如何指定创建文件函数获取未缓存的结果?
- 标准库头文件函数原型的实现是如何用 c++ 编写的?
- 如何将类对象作为对另一个头文件函数的引用传递
- 读取文件函数lpbuffer和字节以读取澄清
- C++ - 未定义的对多个文件函数的引用
- 使用C++的可视 Unicode 文件函数
- C 创建文件函数错误
- WinAPI 创建文件函数 2.
- 具有CREATE_ALWAYS处置的创建文件函数
- 重写c库文件函数
- 如何使用客户端调用堆栈中的地址从 PDB 检索文件/函数/行号?
- 通过头文件函数将遍历的数据从二进制搜索树加载到Vector中
- 需要帮助无法弄清楚为什么我的头文件函数声明不起作用
- 是否可以在不使用文件函数的情况下使用代码块从文件运行某些用例测试?
- 如何用c++文件函数读写8位整数单位形式的数据
- 在c++中读文件函数如何识别文本文件的结束
- 使用Windows API的c++文件函数