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 7 1770 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
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.
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
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 ?
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 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
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, This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thompson, Jordan |
last post by:
I wan to use a variable for the outfile file name in a select into clause. I am
doing something like:
set @fileName="/tmp/result.text"
SELECT col1, col2 INTO OUTFILE @fileName
FIELDS...
|
by: Tom Pope |
last post by:
Is there a way to have the field descriptions put into an outfile?
When I do a Select * from data, the display shows the field names,
however when I do the select * into outfile /tmp/test.txt the...
|
by: solartimba |
last post by:
Hi,
I am in another country, and I need to finish a program. But I do not
have any reference books. Can you tell me how to print this to a file
without ending the stream with an endl. Stated...
|
by: Bruce D |
last post by:
I have query that I export into a file using the "outfile" syntax. I can
use the following lines of code to create a delimited file but I want to
know if I can create a fixed length file? Any...
|
by: pmiller |
last post by:
I ported my code from the development to
application platform, I found a "type error"
on a fileout statement:
outfile.write(object.id +",")
Object.id is provided by a library routine
that is...
|
by: Wamaniyma Akolwa |
last post by:
Hi
I am running MYSQL 5.0 ON WINXP pro.
I have the following procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS LSMW_COST_SWITCH $$
CREATE PROCEDURE LSMW_COST_SWITCH()
BEGIN
|
by: varusnyc |
last post by:
Hello,
Im having really hard time writing an Employee Payroll program that uses functions to read data from file then send all data to another file. I managed to construct some pieces of the code,...
|
by: harrisd |
last post by:
I am attempting to generate a outfile with a unique name using time and date along with a concat of the file name.
The prepare statement throws an syntax error. I have tried single quotes, double...
|
by: chazzy69 |
last post by:
Im currently using a php script to access a database and use the following lines to produce a backup of a table-
$query = "SELECT * INTO OUTFILE 'test.sql' FROM `default_en_listingsdbelements`";...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |