Re: Why can't i write to a file with this code?
- From: Robert Newson <ReapNewsB@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 04 Jul 2006 18:35:41 GMT
s. keeling wrote:
....
nj = read(fd,buffer,100);
You're going to read 100 ints (chars?) into a buffer, and the return
value from read is used as an array subscript to terminate the buffer?
Why?
$ man 2 read
....
RETURN VALUE
On success, the number of bytes read is returned (zero
I presume that read ignores all end of lines, end of strings, etc; ie it reads upto the required number of bytes (less if EOF is encountered, for example). Thus, after reading n bytes from the file, he puts a terminating nul (\0) at the end of them.
or were you wanting him to explain?
....
buffer[nj]='\0';
However, there is a bug in what he's written:
if the read fails for any reason, it'll return -1 and so he'll be splatting a nul over the byte *BEFORE* the buffer - he may be lucky in that it does no damage, but then again, he may not...
.
- Follow-Ups:
- Re: Why can't i write to a file with this code?
- From: s. keeling
- Re: Why can't i write to a file with this code?
- References:
- Why can't i write to a file with this code?
- From: laclac01
- Re: Why can't i write to a file with this code?
- From: s. keeling
- Why can't i write to a file with this code?
- Prev by Date: How to configure sample rate for /dev/dsp?
- Next by Date: A new reader? Welcome to alt.os.linux, read this first if you're new here (FAQ)
- Previous by thread: Re: Why can't i write to a file with this code?
- Next by thread: Re: Why can't i write to a file with this code?
- Index(es):
Relevant Pages
|