We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

C++ AND create a log file

missymadi
missymadi asked
on
Medium Priority
3,487 Views
Last Modified: 2012-08-14
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

Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

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

    Prog1.close;

use

    Prog1.close();
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
BTW, as a side note - you don't need to explicitly call 'close()', this is done automatically when the object goes out of scope.

Author

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

and this will output to a file?
CERTIFIED EXPERT
Top Expert 2012
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2009

Commented:
If you simply want to re-direct all standard output to a log file, you can start the executable like this :

        executable > logfile.txt

Commented:
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.