Advertisement

Log´s

Started by March 03, 2002 04:34 AM
5 comments, last by Gnomo 22 years, 11 months ago
How can I make Log´s?
In C :

      #include <stdlib.h>#include <stdio.h>FILE* log = fopen( "log.txt", "a" ); // appendif( !log ) { exit( EXIT_FAILURE );...fprintf( log, "The number is %d\n", number ); // just like you'd use printf;          


You can also redirect stderr, since some fucntions, like perror(), write there by default.

  #include <stdlib.h>#include <stdio.h>stderr= freopen( "log.txt", "a", stderr ); // yep you reassign to stderrif( !stderr ) { exit( EXIT_FAILURE ); }fprintf( stderr, "The number is %d\n", number );          


In C++

  #include <cstdlib>#include <fstream>using namespace std;ofstream log( "log.txt" );if( !log.good() ) { exit( EXIT_FAILURE ); }...log << "The number is " << number << endl;      


You can try and redirect cerr, but it's UGLY.


Edited by - Fruny on March 3, 2002 5:55:21 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
That doesn´t work.where must i write the code?
Just do :

#include <stdio.h>


FILE *File_Log;

main()
{

if (!fopen("File.txt","w"))
printf("Error\n");
else
printf("File created\n");

for (int i = 0; i < 5; i++)
fprintf(File_Log,"Number %i\n", i);

fclose(File_Log);

return 0;
}




========================
Leyder Dylan
http://ibelgique.ifrance.com/Slug-Production/
========================Leyder Dylan (dylan.leyder@slug-production.be.tf http://users.skynet.be/fa550206/Slug-Production/Index.htm/
Well, of course 'number' must be declared, it is just an example.

Edited by - Fruny on March 3, 2002 8:56:22 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Gnomo : You have some nice logging classes at flipcode (look into COTD)

There are more worlds than the one that you hold in your hand...
You should never let your fears become the boundaries of your dreams.
Advertisement
Thx. Now it works

This topic is closed to new replies.

Advertisement