Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading to the end of medium

19 views
Skip to first unread message

Andrew Smallshaw

unread,
Apr 17, 2013, 2:16:59 PM4/17/13
to
Consider the following code extract to read a file:

int input_fd;
char *input_buf; /* Buffer is malloc()ed elsewhere */
ssize_t input_len;
off_t offset = 0;

/* Open input file */
if ((input_fd = open(filename, O_RDONLY)) == -1) {
fprintf(stderr, "Error opening input file \"%s\": %s\n", filename, strerror(errno));
exit(1);
}

for (;;) {

/* Read input file and handle errors */
input_len = read(input_fd, input_buf, 1048576);
if (input_len == -1) {
fprintf(stderr, "Error reading input file \"%s\": %s\n", filename, strerror(errno));
exit(1);
}

/* Detect end of file */
if (input_len == 0) {
close(input_fd);
return;
}

offset += input_len;
}

This all works fine for a regular file but I'm hitting problems
when using a device node: I'm not getting the very end of the
medium. It appears that the last blocks are not retrieved, so
for example I have an ISO image that is 463667200 bytes when I burn
it to a CD. When I read it back I only get 4096 bytes less than
that. The data I get is fine, it just stops too early. How can
I read the whole lot?

Operating system is NetBSD 6.0, but I suspect this is some generic
issue tripping me up.

--
Andrew Smallshaw
and...@sdf.lonestar.org

Richard Kettlewell

unread,
Apr 19, 2013, 6:52:25 AM4/19/13
to
Is that actually the code you’re using? If so then your first problem
is that you never actually use the value of ‘offset’, just update it.

--
http://www.greenend.org.uk/rjk/

Scott Lurndal

unread,
Apr 19, 2013, 9:51:17 AM4/19/13
to
How is that a problem? read updates the file pointer automatically.

Richard Kettlewell

unread,
Apr 19, 2013, 10:15:28 AM4/19/13
to
>>Is that actually the code you’re using? If so then your first problem
>>is that you never actually use the value of ‘offset’, just update it.
>
> How is that a problem? read updates the file pointer automatically.

Consider what’s in input_buf[0..1048575] and input[1048576...offset-1]
on completion.

--
http://www.greenend.org.uk/rjk/

Scott Lurndal

unread,
Apr 19, 2013, 10:21:54 AM4/19/13
to
He's simply counting the bytes in the file. The contents of input_buf in this
example program are quite irrelevent.

The comment "Buffer is allocated elsewhere" would imply that this code fragment
is constructed as an example, not as his actual code in any case.

Richard Kettlewell

unread,
Apr 19, 2013, 11:47:26 AM4/19/13
to
>>>>Is that actually the code you’re using? If so then your first problem
>>>>is that you never actually use the value of ‘offset’, just update it.
>>>
>>> How is that a problem? read updates the file pointer automatically.
>>
>>Consider what’s in input_buf[0..1048575] and input[1048576...offset-1]
>>on completion.
>
> He's simply counting the bytes in the file. The contents of input_buf
> in this example program are quite irrelevent.
>
> The comment "Buffer is allocated elsewhere" would imply that this code
> fragment is constructed as an example, not as his actual code in any
> case.

The reason I asked whether it’s the actual code he’s running is that if
it isn’t then perhaps the problem he’s asking about lies in the
difference.

--
http://www.greenend.org.uk/rjk/
0 new messages