Click here to Skip to main content
11,430,053 members (73,536 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello all Programmer,

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

thanks
Sampath
Posted 28-Jul-10 4:36am
Edited 16-Aug-10 21:21pm
ThatsAlok91.1K
v3
Comments
Kristian Sixhoej at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Sampath,

#include "stdio.h"
void WriteLogFile(const char* szString)
{
  #IFDEF DEBUG
 
  FILE* pFile = fopen("logFile.txt", "a");
  fprintf(pFile, "%s\n",szString);
  fclose(pFile);
 
  #ENDIF
 
}

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

Good luck!
  Permalink  
Comments
Albert Holguin at 11-Feb-13 10:59am
   
Easy as that.... +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.

Cheers,

Ash

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
  Permalink  
Comments
Albert Holguin at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

you can use the log file library present in codeproject
A Simple LogFile[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v4
Comments
Niklas Lindquist at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Hi
Try it:
#include
#include

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.
  Permalink  
Comments
CHill60 at 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)

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



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 11 Dec 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100