O_TEXT wrote:
[...]
okay, I understand the \r\n -> \n translation mechanism.
But what I do not understand is whether read and lseek values are bytes
offset or character offset.
If you read 4096 bytes which contain 3900 translated characters, will
read return 4096 or 3900? what's about lseek?
Well, open/read/lseek are not part of standard C. However, the
same discussion applies to text mode of fopen/fread/fseek.
I will use MS-Windows as an example, and any specific numbers are
implementation-specific. However, the ideas are cross-platform.
Suppose you have a file containing the following 10 characters:
'a' 'b' 'c' '\r' '\n' '1' '2' '3' '\r' '\n'
If the file is fopened in text mode, and you call fread() with a
length of 5, you will be returned the following 5 characters,
and fread() will tell you that you read 5 characters:
'a' 'b' 'c' '\n' '1'
Note that you get the '1' from the second line of the file. This
is because you requested 5 characters, and there were 5 available.
Note, too, that although 5 characters were returned, you have
advanced 6 characters within the file.
If you were to call ftell(), it would tell you that you were at
position 6 in the file, despite having read only 5 characters.
If you were to later fseek() to the value returned from ftell(),
you would be positioned where you were before -- at the '2'.
However, if you were to assume that, having read 5 characters,
and passed the offset 5 to fseek() (which, as I recall, is not
"legal", as it's not a value returned from ftell), you would
be positioned elsewhere -- at the '1'.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:
[email protected]>