Click here to Skip to main content
15,569,644 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all Programmer,

Can anybody help me to write log file in c++ or provide me the pointer or some code snippet for same.

Updated 30-Aug-22 23:51pm
Kristian Sixhøj 28-Jul-10 9:48am    
I'm sure someone here can, but you have to be a bit more specific. What exactly are you having trouble with?
ThatsAlok 17-Aug-10 2:20am    
If it MFC, you can use CStdioFile, i know it's c++ based question. thats why posting as comment!
The Manoj Kumar 17-Aug-10 2:29am    
One question for you, what is the difference between writing a file and writing a log file? Aren't they same except the information to write to it.

I'll see the C answer given earlier and raise a C++ answer for the same thing:

#include <fstream>

void write_text_to_log_file( const std::string &text )
    std::ofstream log_file(
        "log_file.txt", std::ios_base::out | std::ios_base::app );
    log_file << text << std::end;

It'll do the same thing with the bonus that if the file fails to open for whatever reason it won't crash in a steaming heap of undefined behaviour.



PS the important bits of logging for when you write your own are:

- flush after every message - std::endl does that

- close the file after every message - the fstream destructor does that
Share this answer
Albert Holguin 11-Feb-13 11:01am    
the flushing is very important in logging because otherwise the actual write may be delayed, something that's desirable for performance reasons in regular file write operations but very undesirable when debugging. +5
you can use the log file library present in codeproject
A Simple LogFile[^]
Share this answer
log file is basically a text file. So you can use file writing functions to write logs on to it. Logs normally contains TimeStamp + Error Code + and a small single line description mentioning the problem
Share this answer
Hi Sampath,

#include "stdio.h"
void WriteLogFile(const char* szString)

  FILE* pFile = fopen("logFile.txt", "a");
  fprintf(pFile, "%s\n",szString);



This is the most simple way to append simple log strings to a file if DEBUG is defined.

Good luck!
Share this answer
Albert Holguin 11-Feb-13 10:59am    
Easy as that.... +5
Simply open a text file and redirect the standard output to the file..

Simple Logger in C[^]

Edit: Updated the link to point to the logger.
Share this answer
Niklas L 16-Aug-10 4:41am    
Reason for my vote of 2
One would expect a link to someones own site actually pointing to the solution of the problem.
Hi Try it:
#include <iostream>
#include <cstdio>

int main(int argc, char* argv[])
using namespace std;
freopen( "output.txt", "w", stdout );
freopen( "error.txt", "w", stderr );

cout << "Output message" << endl;
cerr << "Error message" << endl;
Yours Farhad.
Share this answer
CHill60 11-Dec-13 9:30am    
Reasons for my downvote. The question is over 3 years old and already adequately answered; this will not even compile (#include #include)
#define LOG_FILE "Temp.png"
void saveData(std::string data)
std::fstream logFile;
// Open File, std::ios::app);
//Write data into log file
logFile << data;
// close file stream

Share this answer
CHill60 31-Aug-22 5:37am    
You have added nothing new to this 12 year old thread

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900