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: Some ideas about c? Date: Fri, 19 May 2017 10:06:58 -0700 Organization: None to speak of Lines: 49 Message-ID: References: <87bmqp7utb.fsf@gmail.com> <48eb1c1f-c162-45ff-b7ce-74108ee6c522@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="189d31b8aa2df37c9a5da9ba095e96a2"; logging-data="14760"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8OhxN8qlak3oNNdAA8aR8" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:otHRzZ/4WuWugoHx9YfaWnJ/uig= sha1:PO1M9G0jBS4ztm0DTcq/7w12Jf4= Xref: csiph.com comp.lang.c:110392 supercat@casperkitty.com writes: > On Friday, May 19, 2017 at 7:38:13 AM UTC-5, Scott Lurndal wrote: >> Gareth Owen writes: >> >scott writes: >> > >> >> int main() { char buffer[512]; fputs(fgets(buffer, sizeof(buffer), >> >> stdin), stdout); } >> > >> >Great. Except it doesn't work if the input line is too big. >> >> Sure it does. It just truncates it. And the programmer is, of >> course, aware of that. > > It doesn't truncate lines; it wraps them. Sometimes that's desirable, and > sometimes not. A function which behaved like fgets() but provided a choice > of behaviors (truncate, wrap, or trap/signal) would have been helpful, but > fgets is what it is. No, it doesn't wrap. The program reads and prints a single line, discarding any remaining input. If the line is longer than 512 characters, including the trailing newline, it prints only the first 512 characters and discards the rest. (Which means that the output might not contain a terminating newline even if the input does.) If the fputs(fgets(...)) call were in a loop, rather than being called just once, it would copy the entire content of stdin to stdout. Remember that fputs() doesn't add a newline. The original problem statement was: Write a program, that reads in a line from the console and then echoes the same line to the console again. The program assumes, perhaps incorrectly, that stdin and stdout are associated with "the console". A correct implementation of that requirement could read a character at a time, stopping after printing a '\n' or reaching end-of-file. If course if you need to do some additional processing on each line, it gets a bit more complicated. -- 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"