2

私の無知を許してください..私はかなりのことを知っていますが、どういうわけか基本についてまだ漠然としています!?!この簡単な例を考えて、ログメッセージを「writeLogFile」に渡す最良の方法を教えてください?

void writeLogFile (ofstream *logStream_ptr) 
{  
    FILE* file;
    errno_t err;

    //will check this and put in an if statement later..
    err = fopen_s(&file, logFileName, "w+" );


    //MAIN PROB:how can I write the data passed to this function into a file??

    fwrite(logStream_ptr, sizeof(char), sizeof(logStream_ptr), file);


    fclose(file);

}

int _tmain(int argc, _TCHAR* argv[])
{

    logStream <<"someText";

    writeLogFile(&logStream); //this is not correct, but I'm not sure how to fix it

    return 0;
}
4

1 に答える 1

3

の代わりに、タイプofstreamを使用する必要があります。FILE

void writeLogFile ( FILE* file_ptr, const char* logBuffer ) 
{  
   fwrite(logBuffer,1, sizeof(LOG_BUF_MAX_SIZE), file);
}

int _tmain(int argc, _TCHAR* argv[])
{
    writeLogFile(m_pLogFile, "Output"); 
    return 0;
}

他の場所

m_pLogFile = fopen("MyLogFile.txt", "w+");

または、ofstreams のみを使用できます。

void writeLogFile ( const char* logBuffer ) 
{  
   m_oLogOstream << logBuffer << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    writeLogFile("Output"); 
    return 0;
}

他の場所

m_oLogOstream( "MyLogFile.txt" );

以下のコメントに基づいて、あなたがやりたいと思うことは次のようなものです:

void writeLogFile ( const char* output) 
{  
    fwrite(output, 1, strlen(output), m_pFilePtr);
}

int _tmain(int argc, _TCHAR* argv[])
{
    stringstream ss(stringstream::in);
    ss << "Received " << argc << " command line args\n";
    writeLogFile(m_pLogFile, ss.str().c_str() ); 
    return 0;
}

C スタイルの文字列と生のポインター (char と FILE の両方) を扱っているため、ここで説明したよりも多くのエラー チェックが必要であることに注意してください。

于 2012-06-07T09:38:00.653 に答える