Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > advanced printf() and scanf()

Reply
Thread Tools

advanced printf() and scanf()

 
 
Gaijinco
Guest
Posts: n/a
 
      04-25-2006
I read somewhere that printf and scanf had "advanced features" and they
point to:

scanf("%[^\n]",line); // line is a string

as an example, where scanf() acts like gets()

I try to look of more of this "advanced features" in documentation but
couldn't found any. What "advance features" do you of these functions
(printf and scanf)?

Thanks a lot!

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      04-25-2006

Gaijinco wrote:
> I read somewhere that printf and scanf had "advanced features" and they
> point to:
>
> scanf("%[^\n]",line); // line is a string


This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.

> as an example, where scanf() acts like gets()


Making anything behave like `gets()` must be EVIL!

Think of what happens when there's more charaters in the input buffer
than you allowed for in your string buffer.

You're better off using `fgets()` and `sscanf()`.

> I try to look of more of this "advanced features" in documentation but
> couldn't found any.


Try the C Standard. You should be able to download the current version
of C99 plus TCs as N1124.pdf. Google for it.

> What "advance features" do you of these functions (printf and scanf)?


In my line of work, exactly none (don't even use the functions, let
alone their "advanced" features).

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      04-25-2006
In article <(E-Mail Removed) .com>,
Gaijinco <(E-Mail Removed)> wrote:

>I try to look of more of this "advanced features" in documentation but
>couldn't found any. What "advance features" do you of these functions
>(printf and scanf)?


I don't know if you'd call it an "advanced feature", but I have seen
examples that would have been a lot simpler if someone had known about
the use of "*" as the precision modifier.

Suppose that "s" points to an array of characters whose length is
"len" - a representation of a string that does not use null-termination.
This can be printed with

printf("%.*s", len, s);

but I have seen programs where the author malloced a buffer, copied
the string, and null-terminated it so that he could use "%s".

-- Richard
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-25-2006
In article <(E-Mail Removed) .com>,
Vladimir Oka <(E-Mail Removed)> wrote:

>Gaijinco wrote:
>> I read somewhere that printf and scanf had "advanced features" and they
>> point to:


>> scanf("%[^\n]",line); // line is a string


>This tells `scanf()` to read everything until the newline. Newline is
>left in the input buffer, and may be read by subsequent calls.


>> as an example, where scanf() acts like gets()


As the scanf() form leaves the newline in the input stream, but
the gets() form does not, the two are not equivilent.

You could use:

scanf("%[^\n]\n",line);

but you should be sure you understand the result you would get
if end of file occurs before you encounter a \n .
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      04-27-2006

In article <e2lfg7$h3o$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> >Gaijinco wrote:
> >> as an example, where scanf() acts like gets()

>
> As the scanf() form leaves the newline in the input stream, but
> the gets() form does not, the two are not equivilent.
>
> You could use:
>
> scanf("%[^\n]\n",line);
>
> but you should be sure you understand the result you would get
> if end of file occurs before you encounter a \n .


Also, since \n is a whitespace directive for the fscanf family, it
will consume not only the newline (if it exists) but any following
whitespace. If this scanf is used in a loop, for example, it will
remove leading whitespace from all lines after the first. So again,
this is not equivalent to gets().

(It *is* similar to gets() in that, as presented, it can overflow
the line object. There should be a maximum size specifier on that
conversion directive.)

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

Even though there may be some misguided critics of what we're trying
to do, I think we're on the wrong path. -- Reagan
 
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
Re: Advanced Python Programming Oxford Lectures [was: Re: *Advanced*Python book?] Michele Simionato Python 1 03-27-2010 06:10 AM
Announce: BGP and Advanced Routing Resources jacco@bgp4.as Cisco 2 09-08-2009 02:22 AM
Voip Translating Basic and Advanced kimi Cisco 0 03-21-2006 01:29 PM
Advanced Guides: VPNs and Internet Connection Security Silverstrand Front Page News 0 11-12-2005 06:21 PM
Advanced replacement and SmartNet Marco Matarazzo Cisco 3 06-25-2004 12:51 PM



Advertisments