C++ AND create a log file

Experts,

      How would I create a log file in C++ and record the cout's to the screen in a .txt or .log file?
I looked up ofstream but I'm not sure how to capture ALL the cout's througout my program. I'm getting an error in the main function right now that I am testing:

c:\test\proj1\proj1\proj1.cpp(65) : warning C4018: '<' : signed/unsigned mismatch
1>c:\test\proj1\proj1\proj1.cpp(133) : error C3867: 'std::basic_ofstream<_Elem,_Traits>::close': function call missing argument list; use '&std::basic_ofstream<_Elem,_Traits>::close' to create a pointer to member
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>Build log was saved at "file://c:\test\PROJ1\PROJ1\Debug\BuildLog.htm"
1>PROJ1 - 1 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

int main()
{
	ofstream Prog1;
	Prog1.open ("Prog1_output.txt");
	Prog1 << "Testing.\n";
	
 
	// Read the input file and load all processes onto LTWQ
	ReadInputFile();
 
	// Get out master start tick count
	MasterStartTickCount = GetTickCount();
 
	// While there are still PCBs on the LTWQ
	while(!LTWQ.empty())
	{
		// Pop them off and run them
		ProcessLTWQ();
 
		ProcessReadyQ();
	}
    Prog1.close;
	return 0;
}

Open in new window

missymadiAsked:
Who is Participating?
 
jkrCommented:
Well, for that you would need to make that a global variable, e.g.

ofstream Prog1;  // global object
 
int main()
{
        Prog1.open ("Prog1_output.txt");
        Prog1 << "Testing.\n";
        
 
        // Read the input file and load all processes onto LTWQ
        ReadInputFile();
 
        // Get out master start tick count
        MasterStartTickCount = GetTickCount();
 
        // While there are still PCBs on the LTWQ
        while(!LTWQ.empty())
        {
                // Pop them off and run them
                ProcessLTWQ();
 
                ProcessReadyQ();
        }
    Prog1.close();
        return 0;
}

Open in new window

0
 
jkrCommented:
You're missing the parenthesis in your call to 'close()'. Instead of

    Prog1.close;

use

    Prog1.close();
0
 
jkrCommented:
BTW, as a side note - you don't need to explicitly call 'close()', this is done automatically when the object goes out of scope.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
missymadiAuthor Commented:
So throughout my code I can use << "Blah Blah Blah\n";

and this will output to a file?
0
 
Infinity08Commented:
If you simply want to re-direct all standard output to a log file, you can start the executable like this :

        executable > logfile.txt
0
 
elqnoCommented:
i don't know if u better would like a code for a log file, i made it, as simple as i could, coz was for me, i was bored one time and i did a small library for my own purposes :P and now i want to share it might be usefull ;)

it's made with a header (.h) file and a c++ file (cpp), there is an example additional, so u don't need to try to understand the code if u don't want to, but if ur purpose about this log file is how to build one, i hope this code will help you, in your future codes ;)
//The log.cpp file
#include <string>
#include <time.h>
 
log_class::log_class()
{
          this->text = "";
}
 
void log_class::savelog(char *x,char o)
{
     if (o == 'y') remove(x);
     ofstream log;
     log.open (x, ios::out | ios::app);
     log << this->text << "\n";
     log.close();
}
 
void log_class::push(string x)
{
     char *dateStr = new char[9];
     char *timeStr = new char[9];
     _strdate(dateStr);
     _strtime(timeStr);
     this->text += "[";
     this->text += dateStr;
     this->text += " ";
     this->text += timeStr;
     this->text += "]: " + x + "\n";
}
 
//the log.h file
/*
 
                Log File Created by: Emmanuel Defranc
Description: It was a boring day :P, all the functions of how to use it
             is on the example_log.cpp file ;)
*/
 
class log_class
{
      private:
              string text;
      public:
             log_class();
             void savelog(char *,char);
             void push(string);
};
 
log_class log;
 
//the example.cpp file
/*
 
                Log File Created by: Emmanuel Defranc
Description: It was a boring day :P, all the functions of how to use it
             is on the example_log.cpp file ;)
 
*/
 
#include <iostream> //obligatory include
using namespace std;
#include "log.cpp"  //include for the logfile
 
int main()
{
    //puting some text, each call of the function is a new line
    log.push("this is the first text");
    log.push("this is the 2nd text");
    //function for save the log, with the name of the file like argument
    //remember to put the extension for the file too, like down in the example
    //function ("name_of_the_file.extension",'y/n'), 2nd arg = to overwrite the file
    log.savelog("sieci_log.txt",'y');
    return 0;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.