Deutsch English Français Italiano |
<877cep4j2i.fsf@nosuchdomain.example.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: Whaddaya think? Date: Sat, 15 Jun 2024 21:17:57 -0700 Organization: None to speak of Lines: 42 Message-ID: <877cep4j2i.fsf@nosuchdomain.example.com> References: <666ded36$0$958$882e4bbb@reader.netnews.com> <20240616015649.000051a0@yahoo.com> <v4lm16$3s87h$4@dont-email.me> <v4lmso$3sl7n$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sun, 16 Jun 2024 06:17:58 +0200 (CEST) Injection-Info: dont-email.me; posting-host="2de609396a989ebb89e0552bebaeb129"; logging-data="4078367"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lyZpFBcj0v9OrEYwfe785" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:d4waM8fftMC97gEnpsEZOVuP/Rk= sha1:OZ9UUim1DGcXu5PkfVlBHpRphuI= Bytes: 3180 Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes: > On 16.06.2024 05:26, Lawrence D'Oliveiro wrote: >> On Sun, 16 Jun 2024 01:56:49 +0300, Michael S wrote: >> >>> If you want to preserve you sanity, never use fscanf(). >> >> Quoth the man page <https://manpages.debian.org/3/scanf.3.en.html>: >> >> It is very difficult to use these functions correctly, and it is >> preferable to read entire lines with fgets(3) or getline(3) and >> parse them later with sscanf(3) or more specialized functions such >> as strtol(3). > > This would be also my first impulse, but you'd have to know > _in advance_ how long the data stream would be; the function > requires an existing buffer. So you'd anyway need a stepwise > input. On the plus side there's maybe a better performance > to read large buffer junks and compose them on demand? But > a problem is the potential cut of the string of a number; it > requires additional clumsy handling. So it might anyway be > better (i.e. much more convenient) to use fscanf() ? I advise never using any of the *scanf() functions for numeric input unless you have control over what appears on the input stream. They have undefined behavior if the input value is out of range. (I consider this a bug in the language.) Typical behavior is to store some arbitrary integer value with no error indication. As for reading lines, getline() can allocate a buffer long enough to hold the line, but it's defined by POSIX, not by ISO C. For the original problem, where the input consists of digits and whitespace, you could read a character at a time and accumulate the value of each number. (You probably want to handle leading signs as well, which isn't difficult.) That is admittedly reinventing the wheel, but the existing wheels aren't entirely round. You still have to dynamically allocate the array of ints, assuming you need to store all of them rather than processing each value as it's read. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */