473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

use fprintf to write to the console

hi all,

i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.
how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.

Nov 14 '05 #1
4 4933
"baumann@pan" <ba*********@gmail.com> wrote:
if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.
You mean, to stderr?
how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.


There is no "error std console" with any int value, even 2, in ISO C.
You may be thinking of POSIX.

stderr is a FILE *.

Richard
Nov 14 '05 #2
baumann@pan wrote:

hi all,

i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.

how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.


#include <stdio.h>

int main(void)
{
fprintf(stderr, "%s",
"if not defined LOG_TO_FILE, "
"i want to simply write to error std console.\n"
"how can I do so with fprintf since fprintf only "
"accept FILE*?\n"
"whileerror std console has int value 2.\n"
);
return 0;
}

--
pete
Nov 14 '05 #3
"baumann@pan" wrote:

i want to use the fprintf to make log.

if defined LOG_TO_FILE, it is easy to use fprintf to write the log
file.

if not defined LOG_TO_FILE, i want to simply write to error std
console.

how can I do so with fprintf since fprintf only accept FILE*? while
error std console has int value 2.


#define LOG_TO_FILE 1 /* or 0, or leave undefined */
.....
FILE *reporterrs;
.....
if (LOG_TO_FILE) {
reporterrs = fopen(......);
}
else reporterrs = stderr;
.....
/* use reporterrs */
if (LOG_TO_FILE) fclose(reporterrs);

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Nov 14 '05 #4
In article <42***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
#define LOG_TO_FILE 1 /* or 0, or leave undefined */ if (LOG_TO_FILE) {


Small slip there: if you leave LOG_TO_FILE undefined, then in the
if statement, it is going to be treated as an undefined variable
with compiler-dependant results.

I suspect it momentarily slipped your mind that treating
an undefined macro as the value 0 only occurs while evaluating
preprocessing conditional expressions.
--
"Who Leads?" / "The men who must... driven men, compelled men."
"Freak men."
"You're all freaks, sir. But you always have been freaks.
Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joy_julia446 | last post by:
Hi there, I tried to write to an output file like: Hello USER_NAME You are visitor no:COUNT USER_NAME : string and can be of any length bt 1 and 30. You: The char 'Y' has fixed...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
6
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s...
11
by: nertos | last post by:
Why doesn't this fragment work? FILE *file; fopen(argv, "r+"); fprintf(file, "\b"); THX nertos
2
by: noleander | last post by:
Sorry if this is a trivial question :-) I've got several print statements in my code: fprintf ( stderr, ....); but when I run my program, I cannot find the output text in any window. My...
1
by: samac | last post by:
Hi all, Need some help in c. I have not been in programming for ten years. Here is my problem: From main(), I call a function - a_function(). In a_function(), I call another function...
16
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf...
11
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At...
5
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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...

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.