Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ofstream::write()未处理的异常_C++_String_File Io_Unhandled Exception_Ofstream - Fatal编程技术网

C++ ofstream::write()未处理的异常

C++ ofstream::write()未处理的异常,c++,string,file-io,unhandled-exception,ofstream,C++,String,File Io,Unhandled Exception,Ofstream,所以我正在编写一个日志库来记录各种事情,当我对它进行测试时,它不断崩溃。当我将日志消息写入ofstream文件时,我将异常范围缩小到写入函数。我解析消息和内容,然后实际调用ofstream::write()。以下是我遇到团聚错误的部分: void Logger::writeMessage(LogMessage* message) { if(message==NULL) return; char buffer[MAX_PATH]; switch(messag

所以我正在编写一个日志库来记录各种事情,当我对它进行测试时,它不断崩溃。当我将日志消息写入ofstream文件时,我将异常范围缩小到写入函数。我解析消息和内容,然后实际调用ofstream::write()。以下是我遇到团聚错误的部分:

void Logger::writeMessage(LogMessage* message)
{
    if(message==NULL)
        return;
    char buffer[MAX_PATH];
    switch(message->GetMessageType())
    {
    case LOGMESSAGE_HEADER:
        sprintf(buffer, m_logInfo->headerFormat, message->GetMessage().c_str());
        break;
    case LOGMESSAGE_FOOTER:
        sprintf(buffer, m_logInfo->footerFormat, message->GetMessage().c_str());
        break;
    case LOGMESSAGE_DEBUG:
        sprintf(buffer, "%s %s", m_logInfo->debugPrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_ADDRESS:
        sprintf(buffer, "%s %s", m_logInfo->addressPrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_VALUE:
        sprintf(buffer, "%s %s", m_logInfo->valuePrefix.c_str(), message->GetMessage().c_str());
        break;
    case LOGMESSAGE_CUSTOM:
    default:
        sprintf(buffer, "test!", message->GetMessage().c_str());
        break;
    }
    try
    {
        if(!m_ofile.is_open() || !m_ofile.good())
            return;

        //string formattedMessage(buffer);
        //formattedMessage.append(m_logInfo->lineTerminator);

        string result;
        if(message->IsUsingTimestamp())
        {
            m_ofile << message->GetTimeStamp().GetTimeString().c_str() << " ";
            //result.append(message->GetTimeStamp().GetTimeString());
            //result.append(" ");
        }

        m_ofile << buffer << m_logInfo->lineTerminator;

        //result.append(formattedMessage);
        //result.push_back('\0');

        //m_ofile.write(result.c_str(), MAX_PATH);
        //m_ofile << result.c_str();
    } 
    catch(std::exception &e)
    {
        MessageBox(NULL, e.what(), "ERROR", NULL);
    }
}
void记录器::writeMessage(日志消息*消息)
{
如果(消息==NULL)
回来
字符缓冲区[最大路径];
开关(message->GetMessageType())
{
案例日志消息头:
sprintf(buffer,m_logInfo->headerFormat,message->GetMessage().c_str());
打破
案例日志消息\u页脚:
sprintf(buffer,m_logInfo->footerFormat,message->GetMessage().c_str());
打破
案例日志消息\u调试:
sprintf(缓冲区,“%s%s”,m_logInfo->debugPrefix.c_str(),message->GetMessage().c_str());
打破
案例日志信息地址:
sprintf(缓冲区,“%s%s”,m_logInfo->addressPrefix.c_str(),message->GetMessage().c_str());
打破
案例日志信息_值:
sprintf(缓冲区,“%s%s”,m_logInfo->valuePrefix.c_str(),message->GetMessage().c_str());
打破
案例日志消息\u自定义:
违约:
sprintf(buffer,“test!”,message->GetMessage().c_str());
打破
}
尝试
{
如果(!m_of ile.is_open()| |!m_of ile.good())
回来
//字符串格式化消息(缓冲区);
//formattedMessage.append(m_logInfo->lineTerminator);
字符串结果;
如果(消息->正在使用时间戳())
{
m_文件GetTimeStamp().GetTimeString().c_str()GetTimeStamp().GetTimeString());
//结果。追加(“”);
}
m_of ilec_str()函数返回一个指针,这可能会导致ostream输出出现问题


除非有必要将它转换成该块之外,否则您是否从多个线程访问流?FWIW,<代码> 0xFEEEEFEE < /C>是MSVC调试模式启动代码中的空闲堆分配内存,查看问题何时消失。此外,C++异常与访问违规无关。正在读取。我没有访问冲突的异常处理。ofstream对象不支持std::string类型,因此据我所知,string类内部的转换是必需的。@Zayats ofstream对象确实支持

std::string
s的输出。你忘了
#include
?它们支持吗?这是新的。我确实包括了。它不管我通过C++字符串还是C字符串,都会遇到同样的错误。
__thiscall _Lockit::_Lockit(int kind)
    : _Locktype(kind)
    {   // lock the mutex
    if (_Locktype < MAX_LOCK)
        _Mtxlock(&mtx[_Locktype]);
    }
m_ofile << "test!";