473,666 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OUTFILE - like creating a log?

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
7 1786
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
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("Thi s 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
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
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
On 11 Dec 2006 14:38:38 -0800, in comp.lang.c , "Spiros Bousbouras"
<sp****@gmail.c omwrote:
>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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1477
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 TERMINATED BY ',' FROM test_table; the interpreter dies at the @fileName... if I exchange @filename with "/tmp/result.text" it runs fine.
1
3266
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 field names are not in the file. Thanks for the help Tom
2
1634
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 differently, I don't want an endl after the last data point printed to the file. I know this is a basic question, and I apologize. ******************************
2
3037
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 ideas? delimited file: Select GivenName, Surname, Address, City, State, ZipCode into outfile 'xx.csv' fields terminated by ',' lines terminated by '\r\n' from kbm where zipcode = '12345'
3
2957
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 installed on both machines. How do I fix this ?
0
3900
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
2
5217
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, but I cant figure out how to put it together. Here's the description of whats needed to be done: Write a program that generates an Employee Payroll. All output should go to payroll.out. The input for the program should be read from a file,...
8
6339
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 quotes and combinations of each to no success. What am I missing? Can the outfile even be dynamically named as I'd like? ------------------------------------------------------- SELECT @sync:=concat(CURDATE(),CURTIME()); SET @marcFile =...
6
4498
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`"; $result = mysql_query($query); Which seems to execute fine and produces no errors except it doesn't actually produce any file. I figured it would produce the file in the same directory as the script or if not in the root directory but...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.