-3

I have to write a program for school that calculates current, voltage, and efficiency. I have almost finished the program but now I want to write the results in a logfile. I have already read some threads but it didn't really help. here is the part that I want to write in a logfile:

cout<<"Die spannung U1 betraegt"<<U1<<"Ohm."<<endl;

I would really appreciate help thanks.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130

2 Answers2

1

Simply using File I/O in C++ locally should solve your issue:

#include <fstream>
//...
ofstream fout("logfile.txt");
if (fout){
   fout << "Die spannung U1 betraegt" << U1 << "Ohm." <<endl;
   fout.close();
}

However, logging can become very cumbersome, so people have come up with all kinds of solutions for loggers. I found this article on logfiles (In context of the Singleton design pattern) to be very useful.

AndyG
  • 39,700
  • 8
  • 109
  • 143
0

I would recommend using FILE and fprintf.

http://pic.dhe.ibm.com/infocenter/tpfhelp/current/index.jsp?topic=%2Fcom.ibm.ztpf-ztpfdf.doc_put.cur%2Fgtpc2%2Fcpp_fprintf-printf-sprintf.html

Remember - if you have threads - you need to protect the object,
don't forget to fflush() when the content is meaningful, and to fclose when you're done.

There are other methods to do it- I presonally like the bare bone the most..

evenro
  • 2,626
  • 20
  • 35