gpt4 book ai didi

c++ - 调用 fcout 时有时出现未处理的异常

转载 作者:行者123 更新时间:2023-11-30 05:35:01 25 4
gpt4 key购买 nike

我有这个功能可以将一些日志信息写入文件

int Comm::saveInfo(LPTSTR msg)
{
#ifdef _log
wofstream fcout(LogFile, ios::out | ios::app);
if (fcout.is_open())
{

fcout << msg << "\t" << (int) ::GetTickCount64() << endl;
fcout.close();
}
#endif
return 0; // this is where visual studio 2012 is pointing to for the crash
}

这个函数有时会工作但有时会崩溃...... :(

我是这样调用这个函数的:

saveInfo(TEXT("set measurement time..."));
ret = readMsg(Buf, WAITTIMOUT);

Comm 类的头文件中,我将 std::wstring LogFile; 声明为私有(private)成员。 readMsg 是一个读取管道的函数

int CpipeComm::readMsg(LPTSTR chBuf, DWORD timeout)
{

OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
memset(&chBuf[0], 0, sizeof(chBuf));

int err = 0;
ULONGLONG time1 = ::GetTickCount64();
DWORD bytesRead = 0;
do
{
// Read from the pipe.
fSuccess = ReadFile(
hPipe, // pipe handle
chBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
&overlapped); // not overlapped

return 0;
}

在 Comm 类的构造函数中我做

 LogFile.assign(L"Pipe_log.log");

我得到的随机错误是;

Unhandled exception at 0x000007FEF782AFD3 (msvcp110.dll) in : 0xC0000005: Access violation reading location 0x0000000000000012.

我不确定我是否做错了什么!不知何故,一个接一个地拥有这两个功能导致了崩溃!如果我将 saveInfo 函数移动到另一个地方,一切正常......

最佳答案

没有以 std::wstring 作为文件名的有效文件流构造函数。根据您的初始化示例,这看起来像是 LogFile 的类型。

您应该改为像这样构造流:

wofstream fcout( LogFile.c_str(), ios::out | ios::app );

关于c++ - 调用 fcout 时有时出现未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008377/

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