By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

OUTFILE - like creating a log?

P: n/a
All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");

Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Like for example

func1()
printf("This is the first message");

func2()
prinf("this is the second message");

In my log file.
I will have

This is the first message
this is the second message

Dec 11 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
rh*******@gmail.com wrote:
All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");
int fprintf(FILE *stream, const char *format, ...);

i.e. you must do:
fprintf(outfile, "This is the text\n");
or
fprintf(outfile, "%s\n", "This is the text");

(and #include <stdio.hof course)
Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Like for example

func1()
printf("This is the first message");

func2()
prinf("this is the second message");

In my log file.
I will have

This is the first message
this is the second message
You need to pass the value of outfile (pointer to FILE)
to the functions. (Or make it global, but that is not
very extensible, and means your functions won't be
easily separable from the rest of the code.)

--
imalone
Dec 11 '06 #2

P: n/a
Ian Malone wrote:
rh*******@gmail.com wrote:
>All:

I'm trying to create a log file. I now I can be able to do that
by doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");

int fprintf(FILE *stream, const char *format, ...);

i.e. you must do:
fprintf(outfile, "This is the text\n");
or
fprintf(outfile, "%s\n", "This is the text");

(and #include <stdio.hof course)
> Now questions is, I have a bunch of functions, so whenever I
got to a function I would like to write something in my log file, Is
is possible to basically just put the fprintf inside every function,
and the lines will go after the other?
<snip>
You need to pass the value of outfile (pointer to FILE)
to the functions. (Or make it global, but that is not
very extensible, and means your functions won't be
easily separable from the rest of the code.)
Or you could simply call a Logit function from each function, i.e.,

// Untested.

void Logit(const char * msg)
{
FILE * outfile;

if((outfile = fopen("log.txt", "wa")) != NULL)
{
fprintf(outfile, msg);

fclose(outfile);
}
}

You could break the opening/closing out into other functions - so that it
doesn't get open/closed on each call, but then you'd need to make outfile's
scope wider.
Dec 11 '06 #3

P: n/a
On 11 Dec 2006 08:34:16 -0800, in comp.lang.c , rh*******@gmail.com
wrote:
Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?
Simplest way is to fprintf() to stderr in each function, then redirect
stderr to a file with your normal command shell rules.

Otherwise, fopen/fclose the file in main(), make the file handle a
global variable, and off you go.
--
Mark McIntyre

"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 Kernighan
Dec 11 '06 #4

P: n/a
Mark McIntyre wrote:
On 11 Dec 2006 08:34:16 -0800, in comp.lang.c , rh*******@gmail.com
wrote:
Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Simplest way is to fprintf() to stderr in each function, then redirect
stderr to a file with your normal command shell rules.
How do you know that he has access to a shell ?

Dec 11 '06 #5

P: n/a
On 11 Dec 2006 14:38:38 -0800, in comp.lang.c , "Spiros Bousbouras"
<sp****@gmail.comwrote:
>Mark McIntyre wrote:
>On 11 Dec 2006 08:34:16 -0800, in comp.lang.c , rh*******@gmail.com
wrote:
Now questions is, I have a bunch of functions, so whenever I got
to a function I would like to write something in my log file, Is is
possible to basically just put the fprintf inside every function, and
the lines will go after the other?

Simplest way is to fprintf() to stderr in each function, then redirect
stderr to a file with your normal command shell rules.

How do you know that he has access to a shell ?
I don't, and nor does it matter. If he does, the first answer I gave
is good. If he doesn't the second one works.

What was your point?
--
Mark McIntyre

"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 Kernighan
Dec 12 '06 #6

P: n/a

rh*******@gmail.com wrote:
All:

I'm trying to create a log file. I now I can be able to do that by
doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");
No, you can't. For one, fprintf takes 2 arguments,
and for another, well, consider the following...

[tmp]$ cat a.c

#include <stdio.h>

int main(void)
{
FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf(outfile, "This is the text");
return 0;
}
[tmp]$ gcc -Wall -pedantic a.c
[tmp]$ ./a.out
Segmentation fault

Dec 13 '06 #7

P: n/a
pemo wrote:
Ian Malone wrote:
rh*******@gmail.com wrote:
All:

I'm trying to create a log file. I now I can be able to do that
by doing the following:

FILE *outfile;
outfile = fopen("log.txt", "w");

fprintf("This is the text");
int fprintf(FILE *stream, const char *format, ...);

i.e. you must do:
fprintf(outfile, "This is the text\n");
or
fprintf(outfile, "%s\n", "This is the text");

(and #include <stdio.hof course)
Now questions is, I have a bunch of functions, so whenever I
got to a function I would like to write something in my log file, Is
is possible to basically just put the fprintf inside every function,
and the lines will go after the other?

<snip>
You need to pass the value of outfile (pointer to FILE)
to the functions. (Or make it global, but that is not
very extensible, and means your functions won't be
easily separable from the rest of the code.)

Or you could simply call a Logit function from each function, i.e.,

// Untested.

void Logit(const char * msg)
{
FILE * outfile;

if((outfile = fopen("log.txt", "wa")) != NULL)
{
fprintf(outfile, msg);

fclose(outfile);
}
}

You could break the opening/closing out into other functions - so that it
doesn't get open/closed on each call, but then you'd need to make outfile's
scope wider.
I'd rather extend the function to close the file when called with
NULL and to open the file if it is not already opened - easier for the
caller to use that way (can be closed and reopened whenever the
program deems necessary).

void Logit(const char * msg)
{
#define LOGFILE "log.txt"

static FILE * outfile;

if (!msg) {
if (outfile) {
fclose (outfile);
outfile = NULL;
}
return;
}
if (!outfile) {
outfile = fopen (LOGFILE, "w");
if (!outfile) {
printf ("cannot open %s for logging\n", LOGFILE);
return;
}
}
fprintf(outfile, msg);
}

I also consider the above a good example for the poster, as
the poster seems to be in the learning phase and there's no
better time to introduce static variables, #define's and
resource-handling :-)

For better effect, I'd use a variable argument list and
vfprintf to give the caller much more flexibility (what if
you want to log the number of attempts to connect somewhere?
Surely you don't want the caller to have to manually convert
all integers into strings before logging, do you?)

Using a variable argument list is left as an exercise to
the reader :-)

goose,

Dec 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.