Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: scanf used for to get a line Date: Thu, 03 Nov 2016 09:38:56 -0700 Organization: None to speak of Lines: 26 Message-ID: References: <15ea8d6d-0173-491c-b6b5-b1c57dd61769@googlegroups.com> <236c25a6-8c18-4082-ab3a-86eb55fb2408@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="1f4b9cbd880ce3f40faed57282e3b9ef"; logging-data="4348"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+S0MRLQLwAaJVnM5wPVGKH" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:eOa84vQx7lpX0tYbnjwqmK5rmMg= sha1:X01nY+aZFBBSMOvlp9TY2kZRzyo= Xref: csiph.com comp.lang.c:95663 "Charles Richmond" writes: > wrote in message > news:236c25a6-8c18-4082-ab3a-86eb55fb2408@googlegroups.com... >> On Tuesday, 1 November 2016 08:50:54 UTC, asetof...@gmail.com wrote: >>> What about: >>> char a[1024], c; >>> int w, r; >>> r=scanf("%1024[^\n]%c",a,&c); >>> if(r<1)error(); >>> in the case \n is 2 characters? >> >> Scanf to read a line is a poor choice, but you need to understand that a >> text stream, like stdin, will have platform specific line endings mapped >> to a single character represented as '\n' >> > > You might have fewer problems if you read the line with "fgets()" and then > use "sscanf()" to parse out the results. But be careful. sscanf() has undefined behavior on numeric overflow. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"