Is 'ftell()' return value guaranteed to be larger when more characters have been read?

StackOverflow https://stackoverflow.com/questions/14005303

  •  11-12-2021
  •  | 
  •  

Question

I understand, after a careful reading of cplusplus.com's C library reference that "For text streams, the numerical value [returned by ftell()] may not be meaningful"

My question is: Does this mean that I can't be absolutely sure that the value returned when I have, say, read 3000 characters will be larger than the one returned when only 3 characters have been read?

Thanks in advance,

João Silva.

Was it helpful?

Solution

From the C standard:

7.21.9.4 The ftell function
Synopsis
#include <stdio.h>
long int ftell(FILE *stream);
Description
The ftell function obtains the current value of the file position indicator for the stream pointed to by stream. For a binary stream, the value is the number of characters from the beginning of the file. For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read.

(Emphasis mine)

I think it's pretty clear, but if not, leave a comment.

OTHER TIPS

No. It means that e.g. the value returned by ftell for text streams when the file position ist at the end of a file may not be the actual number of bytes in the file. This is due to different line ending conventions and how these are treated on input. So in essence, you shouldn't do math with ftell values, like subtract them from each other, not even subtract 0 in the hope to compute the number of bytes from the beginning of the file to the current position.

It means that the value returned is only suitable to reposition the file position indicator to the same position is was obtained at.

Yes. If ftell()'s numerical value isn't meaningful, then the relationship between two such values is also not meaningful. Presumably the wording of the library documentation allows for file systems that use some form of internal records that may not be numbered in increasing order.

OTOH, I haven't seen a system where the value isn't a byte offset from the file start. On some systems *(like Windows and DOS), there is an adjustment for two-character line endings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top