Thread (3 messages) 3 messages, 3 authors, 2004-07-20

Re: result=read(fd,buffer,200);

From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
Date: 2004-07-20 17:09:33

On Tue, 2004-07-20 17:41:42 +0200, Gerald Emig [off-list ref]
wrote in message [ref]:
According to the documentation provided in info:libc, read 
returns the number of character it actually has read.
Especially if you are using serial ports, the amount read is often fewer
as you expected.
That's a general thing to keep in mind.

Additionally, keep in mind that c_cc[VMIN] will control how long to
wait, too...
Additionally, checking errno is stronly recommended.
As always:)
Use something like the following to read exactly 200 chars:


int read_amount (int iwantread)
This int should have been size_t.
{
	int readpointer, fetch;
...and fetch needs to be ssize_t.
readpointer=0;
while (iwantread>0) {
	fetch=read (fd,&buffer[readpointer],iwantread);
	if (fetch >= 0) {
		readpointer += fetch;
		iwantread -= fetch;
	} else 	if ((errno!=EINTR) && (errno!=EAGAIN)) return -1;
	}
	return iwantread; // should be 0 on success
}
MfG, JBG

-- 
   Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
   ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help