Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question

Reply
Thread Tools

question

 
 
Eric Sosman
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:
>> vprintf() writes to stdout, and cannot be redirected anywhere else.
>> vfprintf() can write to any open output stream, including stdout and
>> stderr. It is most unusual to find anything that writes to stdin.
>>

> fgets writes to stdin maybe that's what I'm thinking of.


Most people would say that it "reads from" stdin ...

> I know advice was
> given to me not to consider the functions begining with f to associated with
> the FILE struct or text or binary files.


There are certainly some f-functions that have nothing to
do with FILE* streams -- fabs() and free(), for example -- so
the initial f is not a sure-fire indicator of file-ness. Maybe
that's what your advisor meant.

On the other hand, most (not all) functions that deal with
FILE* streams have an f somewhere near the start of the name.

> Is vprintf like printf? If so why
> use it? If vfprintf is like fprintf what is the purpose of the v family of
> functions?


Personally I haven't found vprintf() useful; I suspect it's
only there for completeness' sake. It is "like" plain printf()
in that it converts argument values according to a format and sends
them to stdout, but it is different in the way it obtains those
arguments. For ordinary printf() you list the arguments right
there in the call:

printf ("This %s is %s!\n", stuffInTheBowl,
temperature > 90 ? "too hot"
: temperature < 50 ? "too cold"
: "just right");

But now suppose you want to write a function goldilocksSays()
that looks a lot like printf() and is able to do all the same
kinds of formatting, but does a little extra stuff of its own.
So you begin

void goldilocksSays(const char *format, ...) {
printf ("\"");
printf (format, ...); /* need help here */
printf ("\" said Goldilocks.\n");
}

The problem is with that printf() call in the middle: How can
you "pass along" the arguments that the caller provided to
goldilocksSays()? That's where vprintf() comes in: It does the
same thing as printf(), but uses a va_list to encapsulate those
arguments:

void goldilocksSays(const char *format, ...) {
va_list ap;
printf ("\"");

/* here comes the magic: */
va_start (ap, format); /* package the ... arguments */
vprintf(format, ap); /* print them */
va_end (ap); /* dispose of the package */

printf ("\" said Goldilocks.\n");
}

We don't need to know how many or what kinds of arguments the
caller provided for the "..." part of the parameter list, but
through the va_list mechanism we can pass them along anyhow.
And vprintf() -- and vfprintf() and vsprintf() and some of their
younger siblings -- know how to pluck the arguments from the
va_list goldilocksSays() provides, rather than from their own
"..." arguments (which they don't have).

And this, my friend, is why you need a tutorial. A tutorial
first, and a reference eventually if you get serious.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:

> > vprintf() writes to stdout, and cannot be redirected anywhere
> > else. vfprintf() can write to any open output stream, including
> > stdout and stderr. It is most unusual to find anything that writes
> > to stdin.
> >

> fgets writes to stdin maybe that's what I'm thinking of.


You meant "reads from stdin". You can't write to stdin, that's why it
has "in" in its name.

fgets() reads from a text stream opened for reading. stdin is a text
stream open for reading. It is not the only text stream that you can
read from.

> I know
> advice was given to me not to consider the functions begining with f
> to associated with the FILE struct or text or binary files.


Most of the f* functions are indeed for operations on open file
streams. These streams may not be a file on a disk, of course. One of
the parameters will be a FILE*.

There are a few that aren't, like fabs(), floor(), and free(). These
shouldn't be too confusing though.

> Is
> vprintf like printf? If so why use it? If vfprintf is like fprintf
> what is the purpose of the v family of functions?


I gave you a link to a page with information about functions. Did you
look up the v* ones? You'll see that that they are different.

You need to stop guessing. Look things up until you are familiar with
them. Again, I'll tell you that anything else is a waste of your time
and ours.




Brian

 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      01-17-2008
On 17 Jan 2008 at 22:20, Default User wrote:
> Bill Cunningham wrote:
>
>> > vprintf() writes to stdout, and cannot be redirected anywhere
>> > else. vfprintf() can write to any open output stream, including
>> > stdout and stderr. It is most unusual to find anything that writes
>> > to stdin.
>> >

>> fgets writes to stdin maybe that's what I'm thinking of.

>
> You meant "reads from stdin". You can't write to stdin, that's why it
> has "in" in its name.
>
> fgets() reads from a text stream opened for reading. stdin is a text
> stream open for reading. It is not the only text stream that you can
> read from.
>
>> I know
>> advice was given to me not to consider the functions begining with f
>> to associated with the FILE struct or text or binary files.

>
> Most of the f* functions are indeed for operations on open file
> streams. These streams may not be a file on a disk, of course. One of
> the parameters will be a FILE*.
>
> There are a few that aren't, like fabs(), floor(), and free(). These
> shouldn't be too confusing though.


ROFL! The Loser seems to have been shamed into making a post with a
token amount of actual content - not a bad day's work for a humble
troll!

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-18-2008
"Default User" <(E-Mail Removed)> writes:
[...]
> Tutorials are fine, but they are no substitute for a reference book.

[...]

And vice versa.

I wouldn't want to use a language without having some sort of
reference work handy, but I wouldn't want to learn one just from a
reference book. (Ok, I might, but I'm odd that way.)

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments