From: "Charlie Gordon" <gmane@chqrlie.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: how to find the end of piped data?
Date: Tue, 14 Sep 2004 11:03:18 +0200 [thread overview]
Message-ID: <ci6c3e$mra$1@sea.gmane.org> (raw)
In-Reply-To: 41458643.7060907@sit.fraunhofer.de
Dear Richard,
The C library I/O functions are agnostic about whether stdin data is coming
from a file , a pipeor a character device.
getchar() will return EOF upon end of file, closing of the pipe by the
piping process, or end of stream.
Here is how you should write your scanner:
void scanin()
{
int c;
char tmpkey[1024]; /* or whatever size you want */
int tmpcnt = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
tmpkey[tmpcnt] = '\0';
/* perform whatever treatment you want on a line of input */
handle_one_line(tmpkey);
tmpcnt = 0;
} else {
if (tmpcnt < (int)sizeof(tmpkey) - 2) {
rmpkey[tmpcnt++] = c;
}
}
}
You can also use fgets() and check for NULL as the indication for end of
stream, but you will get a slightly different behaviour on truncated lines.
Cheers,
Chqrlie.
main(){int
y=0,x;while(y!=6){x=(y==0)?101:((y==1)?45:((y==2)?97:((y==3)?120:((y==4)?101
:10))));putchar(x);y++;}}
You can simplify this further : one liners should be less than 80
characters!
main(){int
y=0;while(++y<7)putchar(y<2?101:y<3?45:y<4?97:y<5?120:y<6?101:10);}
You can reduce this even further ;-)
prev parent reply other threads:[~2004-09-14 9:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-13 11:36 how to find the end of piped data? Richard Sammet
2004-09-14 6:06 ` Eric Bambach
2004-09-14 8:10 ` Charlie Gordon
2004-09-14 8:45 ` Richard Sammet
2004-09-14 9:03 ` Charlie Gordon [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='ci6c3e$mra$1@sea.gmane.org' \
--to=gmane@chqrlie.org \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).