在C+;中使用ReadFIle()函数时无法读取文件+; 我在使用C++的Read Fiffe()函数读取文件数据时遇到了一些问题(微软可能具体)。
这是我的密码 存档 读那个文件在C+;中使用ReadFIle()函数时无法读取文件+; 我在使用C++的Read Fiffe()函数读取文件数据时遇到了一些问题(微软可能具体)。,c++,visual-c++,file-io,msdn,file-management,C++,Visual C++,File Io,Msdn,File Management,这是我的密码 存档 读那个文件 void ClientA::readPublicKeyOfOfOtherPeer() { HANDLE hFile=NULL; DWORD-dwBytesRead=0; 字节*ReadBuffer=NULL; 重叠ol={0}; hFile=CreateFile(文本(“D:\\My\u Proj\\shared\\PublicKeyB.txt”),//要打开的文件 GENERIC_READ,//打开进行读取 文件\u共享\u读取,//共享以进行读取 NULL,/
void ClientA::readPublicKeyOfOfOtherPeer()
{
HANDLE hFile=NULL;
DWORD-dwBytesRead=0;
字节*ReadBuffer=NULL;
重叠ol={0};
hFile=CreateFile(文本(“D:\\My\u Proj\\shared\\PublicKeyB.txt”),//要打开的文件
GENERIC_READ,//打开进行读取
文件\u共享\u读取,//共享以进行读取
NULL,//默认安全性
打开现有文件,//仅打开现有文件
文件_属性_正常|文件_标志_重叠,//正常文件
空//无属性模板
);
if(hFile==无效的句柄值)
{
_tprintf(文本(“创建文件”);
_tprintf(文本(“终端故障:无法打开文件\%s\”进行读取。\n”);
printf(“错误%x\n”,GetLastError());
返回;
}
if(FALSE==ReadFile(hFile、ReadBuffer、dwPublicKeyLen和lpNumberOfBytesWrite和ol))
{
//显示错误(文本(“读取文件”);
printf(“终端故障:无法读取文件。\n GetLastError=%08x\n”,GetLastError());
闭合手柄(hFile);
返回;
}
如果(dwBytesRead>0&&dwBytesRead您正在将未初始化的指针ReadBuffer传递给ReadFile。您需要一个足够大的缓冲区来接收结果。我认为这是一个错误\u NOACCESS
-您确定读卡器具有访问该文件的权限,并且没有其他东西专门锁定它吗?抓取GetLastError
函数失败后立即执行。所有这些中间的\u tprintf
调用都可能使其被覆盖。此外,您调用\u tprintf
时使用的格式字符串具有%s
说明符,但没有参数。您确定没有得到错误\u IO\u挂起
(0x3E5)相反?这是在异步文件句柄上启动读取时得到的预期错误代码,如下图所示。否,我得到的是0x3e6。是,reader具有访问权限。
void ClientA::SharePublicKey()
{
printf("Sharing Public Key\n");
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 != dwBytesToWrite\n");
}
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("CreateFile\n"));
_tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"));
printf("Error %x\n", GetLastError());
return;
}
if( FALSE == ReadFile(hFile, ReadBuffer, dwPublicKeyLen, &lpNumberOfBytesWritten, &ol) )
{
// DisplayError(TEXT("ReadFile"));
printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", GetLastError());
CloseHandle(hFile);
return;
}
if (dwBytesRead > 0 && dwBytesRead <= dwPublicKeyLen-1)
{
ReadBuffer[dwBytesRead]='\0'; // NULL character
//_tprintf(TEXT("Data read from %s (%d bytes): \n"), argv[1], dwBytesRead);
printf("%s\n", ReadBuffer);
}
else if (dwBytesRead == 0)
{
//_tprintf(TEXT("No data read from file %s\n"), argv[1]);
}
else
{
// printf("\n ** Unexpected value for dwBytesRead ** \n");
}
retrievedPublicByteArray = ReadBuffer;
CloseHandle(hFile);
}