I am a little confused with function fsetpos in the stdio.h library. I want to be to write to different indexes (i.e do not want to write to a file contiguously) in a file. I was considering using fsetpos however the documentation states..
The internal file position indicator associated with stream is set to the position
represented by pos, which is a pointer to an fpos_t object whose value shall have been
previously obtained by a call to fgetpos.
It does not make sense to me that I have to set the position based on the call from fgetpos. Whats the point since it will just set it to the position it is already set at. Or I am I not understanding it correctly ?
From the C11 standard,
fseek
has a similar limitation:The reason is that text streams don't have a one-to-one mapping between the actual bytes of the source and the bytes you would get from
fgetc
; e.g. on windows systems, the newline character in C tends to be translated into a sequence of two binary characters: carriage return, then line feed.Consequently, the notion of arbitrarily positioning a text stream based on a numerical index is fraught with complications and surprises.
In fact, the documentation of
ftell
warnsBinary streams don't have this limitation, although
The above assumes you are working with byte-oriented streams. Wide-oriented streams have additional restrictions. e.g. under
Streams
:and
fsetpos
does more than just set the file position: again from the C11 standard:which makes it more suitable for setting the position in a wide-oriented streams.