В Tue, 21 May 2024 11:29:33 +0200
Laurent Rhelp <laurentrh...@free.fr> пишет:

> pos <- seek(con_in,2,origin="start")
> # We have to repeat the command to return the good amount of read
> # bytes
> print(paste0("pos is not equal to 2, pos = ",pos))

That's because seek() returns the previous position ("before any
move", the one that the help page calls "current"), not the one after
the seek. Fortunately, calling seek(origin = "start") twice with the
same offset doesn't break anything.

> # we are on position 6
> pos <- seek(con_in,0,origin="current")

That's strange. You started at offset 2 and read three bytes. You
should be at offset 5 at this point. For me, seek() returns 5 here, not
6.

> bytes = readBin(con=con_in, what="raw",n = 1)

But after this, we should be on position 6.

> writeBin(  my_string, con=con_in, useBytes = FALSE)

It's described in help(seek) that R maintains two different pointers
for reading and writing a file. You have been reading it, advancing the
read pointer to 6, but the write pointer stayed at offset 0.

Try seek(con_in, seek(con_in, 0, 'current', 'read'), 'start', 'write')
to set the write pointer to the read pointer before issuing writes.
This seems to give me the expected result.

-- 
Best regards,
Ivan

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to