Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #95510

Re: scanf used for to get a line

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: scanf used for to get a line
Date 2016-11-01 15:05 +0000
Organization A noiseless patient Spider
Message-ID <87r36vme9l.fsf@bsb.me.uk> (permalink)
References <15ea8d6d-0173-491c-b6b5-b1c57dd61769@googlegroups.com> <87h97ro2dx.fsf@bsb.me.uk> <50d0493c-77b6-4dce-ac15-d8cf2584f807@googlegroups.com>

Show all headers | View raw


You quoting is all messed up.  People who read >s and attribution lines
should just ignore them is this post.

asetofsymbols@gmail.com writes:

> Ben wrote:
>>Ros wrote:
> #Ros write
>> What about: 
>> char a[1024], c; 
>> int w, r; 
>> r=scanf("%1024[^\n]%c",a,&c); 
>
>
> You need to put 1023 there or the result might not be null terminated. 
>
>> if(r<1)error(); 
>
> There are other conditions that might be considered errors -- for 
> example getting only one result (and no line ending)
>
> #If no line end found means: 
> #1) found EOF, in this case r==1 and 
> #this is ok

Obviously it's up to the application to decide what's an error or not,
but I'd want a general-purpose line-input function to indicate that it
got data, but no terminating newline.  YMMV, of course.

> #or
> #2) the line is > 1024 so 
>
> #scanf would has to return 0 so r==0

No, for a long line, scanf will return 2 (but c won't be '\n').

> #this is for me an error so ok too

Then you need to detect it.

>  or reading nothing 
> at all.
>
> #r==0 error ok
>
>   It's usually better to test for what you want rather than 
> trying to test for all the ways things can go wrong.
>
> #I would get
> # one string, than use sscanf
> #for to get the numbers from 
> #that string

That's a good strategy, but you probably need to check if the line was
truncated.  If the last number was "cut off" you'll get incorrect data
and no indication that it's incorrect.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

scanf used for to get a line asetofsymbols@gmail.com - 2016-11-01 01:50 -0700
  scanf used for to get a line asetofsymbols@gmail.com - 2016-11-01 01:55 -0700
  Re: scanf used for to get a line mark.bluemel@gmail.com - 2016-11-01 03:55 -0700
    Re: scanf used for to get a line "Charles Richmond" <numerist@aquaporin4.com> - 2016-11-03 11:23 -0500
      Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 09:38 -0700
        Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-03 10:08 -0700
          Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 11:29 -0700
            Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-03 14:37 -0700
              Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 16:34 -0700
                Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 10:21 -0700
                Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 11:46 -0700
                Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 15:04 -0700
                Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 16:44 -0700
                Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-04 17:13 -0700
                Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-04 19:12 -0700
                Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-05 14:10 -0700
                Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-05 14:22 -0700
          Re: scanf used for to get a line Tim Rentsch <txr@alumni.caltech.edu> - 2016-11-15 10:57 -0800
            Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-15 12:05 -0800
              Re: scanf used for to get a line jameskuyper@verizon.net - 2016-11-15 14:01 -0800
              Re: scanf used for to get a line Tim Rentsch <txr@alumni.caltech.edu> - 2016-11-16 10:54 -0800
              Re: scanf used for to get a line Richard Damon <Richard@Damon-Family.org> - 2016-11-21 18:01 -0500
                Re: scanf used for to get a line supercat@casperkitty.com - 2016-11-21 15:44 -0800
      Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-03 13:35 -0700
        Re: scanf used for to get a line Keith Thompson <kst-u@mib.org> - 2016-11-03 13:50 -0700
      Re: scanf used for to get a line ImperiusDamian <damianblack.wolf@gmail.com> - 2016-11-05 16:10 -0400
  Re: scanf used for to get a line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 11:39 +0000
    Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-01 05:47 -0700
      Re: scanf used for to get a line Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-01 15:05 +0000
        Re: scanf used for to get a line asetofsymbols@gmail.com - 2016-11-02 01:42 -0700

csiph-web