Mail Archives: djgpp/1996/11/22/18:43:08
> 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 -