www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/22/18:43:08

Date: Fri, 22 Nov 1996 18:23:50 -0500
Message-Id: <199611222323.SAA01412@delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: gorman AT gpu DOT srv DOT ualberta DOT ca
CC: djgpp AT delorie DOT com
In-reply-to: <574j6m$ptc@pulp.ucs.ualberta.ca> (gorman@gpu.srv.ualberta.ca)
Subject: Re: read() question!!!!!!!!!

> main()
> {
>   int File = open(Filename, O_RDONLY);

Try O_RDONLY|O_BINARY instead of just O_RDONLY if you are reading
binary data.  Otherwise, read() is allowed to return less bytes if the
text conversion requires it.

>   char Buffer[20] = read(File, Buffer, 19);

You mean this:

	int r;
	char Buffer[20];
	Buffer[19] = 0;
	r = read(File, Buffer, 19);
	write(1, Buffer, r);

>   puts(Buffer);

Where is the trailing NULL?

> DJGPP is the bestt

Try write(1, Buffer, 20) instead of puts(), except that the 20 is
wrong also.

> For some reason there is an extra 't' in the example. When I read a
> lot of characters (say 80 or so), there are 3 extra characters!! What
> am I doing wrong here??

Might be dropping the CR/LF down to a LF if the file includes
newlines.  Each line would account for an extra character that
gets dropped.  The dropping algorithm does it in-place, so the
end of the buffer has a copy of the last N bytes.

I think your problem can be solved if (1) you remember about the
trailing NULLs, and (2) don't assume that reading a *text* file will
return as many bytes as you asked for.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019