Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf question

Reply
Thread Tools

sscanf question

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-03-2008
Ben Bacarisse <(E-Mail Removed)> writes:

> http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>
>> On Aug 3, 11:02 am, Chris Torek <(E-Mail Removed)> wrote:
>> <snip>
>>> (It is possible, but somewhat difficult, to "read a line" with
>>> the scanf family. The code to do this is somewhat obscure. It
>>> does appear here in comp.lang.c now and then.)

>>
>> I'd say it's impossible in robust code; the stream can have unwanted
>> embedded null bytes, which scanf will happily read.

>
> That is not a problem. Given:
>
> char line[101], nl[2];
> int nchars;
>
> The call:
>
> scanf("%100[^\n]%n%[\n]", line, &nchars, nl)


I missed the 1 in the %1[\n] format, sorry. Anyway, you get the idea...

> tells us all we need to know. If the return is 2 we saw a whole
> line. If the return is 1 it is partial. In both cases, nchars is the
> number of characters read (excluding a newline if present) and will
> happily include nulls in this count.


--
Ben.
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      08-03-2008
On Aug 3, 4:27 pm, CBFalconer <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Chris Torek <(E-Mail Removed)> wrote:

>
> > <snip>

>
> >> (It is possible, but somewhat difficult, to "read a line" with
> >> the scanf family. The code to do this is somewhat obscure. It
> >> does appear here in comp.lang.c now and then.)

>
> > I'd say it's impossible in robust code; the stream can have
> > unwanted embedded null bytes, which scanf will happily read.

>
> So? A null byte is not a digit, nor a period, so it will normally
> be treated as marking the end of a numeric field.


What are you talking about?
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      08-03-2008
On Aug 3, 4:57 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Ben Bacarisse <(E-Mail Removed)> writes:
> > (E-Mail Removed) writes:

>
> >> On Aug 3, 11:02 am, Chris Torek <(E-Mail Removed)> wrote:
> >> <snip>
> >>> (It is possible, but somewhat difficult, to "read a line" with
> >>> the scanf family. The code to do this is somewhat obscure. It
> >>> does appear here in comp.lang.c now and then.)

>
> >> I'd say it's impossible in robust code; the stream can have unwanted
> >> embedded null bytes, which scanf will happily read.

>
> > That is not a problem. Given:

>
> > char line[101], nl[2];
> > int nchars;

>
> > The call:

>
> > scanf("%100[^\n]%n%[\n]", line, &nchars, nl)

>
> I missed the 1 in the %1[\n] format, sorry. Anyway, you get the idea...
>
> > tells us all we need to know. If the return is 2 we saw a whole
> > line. If the return is 1 it is partial. In both cases, nchars is the
> > number of characters read (excluding a newline if present) and will
> > happily include nulls in this count.


nchar is indeed the number of characters/bytes read. There needs to be
another check like

if(strlen(line) != nchar) /* embedded lunn bytes */

As you see, `line' is processed twice, which may be unwanted in
'robust' code.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-04-2008
On Sun, 3 Aug 2008 14:25:15 -0700 (PDT), (E-Mail Removed) wrote:

>On Aug 3, 4:27 pm, CBFalconer <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Chris Torek <(E-Mail Removed)> wrote:

>>
>> > <snip>

>>
>> >> (It is possible, but somewhat difficult, to "read a line" with
>> >> the scanf family. The code to do this is somewhat obscure. It
>> >> does appear here in comp.lang.c now and then.)

>>
>> > I'd say it's impossible in robust code; the stream can have
>> > unwanted embedded null bytes, which scanf will happily read.

>>
>> So? A null byte is not a digit, nor a period, so it will normally
>> be treated as marking the end of a numeric field.

>
>What are you talking about?


Well, you said '\0' characters would pose some difficulty. He said
they wouldn't.

So let's start at the beginning. Why do you think they prevent robust
code from using scanf? Before you answer, I suggest you look through
the archives for posts by Dan Pop that describe exactly how to do
this.

--
Remove del for email
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-04-2008
(E-Mail Removed) wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Chris Torek <(E-Mail Removed)> wrote:

>>
>>> <snip>
>>>
>>>> (It is possible, but somewhat difficult, to "read a line" with
>>>> the scanf family. The code to do this is somewhat obscure. It
>>>> does appear here in comp.lang.c now and then.)
>>>
>>> I'd say it's impossible in robust code; the stream can have
>>> unwanted embedded null bytes, which scanf will happily read.

>>
>> So? A null byte is not a digit, nor a period, so it will normally
>> be treated as marking the end of a numeric field.

>
> What are you talking about?


The handling of null (i.e. '\0') bytes by the scanf family.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
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
sscanf question sieg1974@yahoo.com C Programming 5 11-23-2006 07:39 PM
Programmer wannabee question about sscanf broeisi C Programming 10 03-05-2006 05:16 AM
sscanf() question? jchludzinski@gmail.com C Programming 5 06-09-2005 05:45 PM
sscanf fixed-width integer question Brent Lievers C Programming 8 04-26-2004 05:55 PM
Question on sscanf Dan Smith C Programming 3 08-14-2003 12:42 AM



Advertisments