Re: [Rd] writting null (\000 or ^@) to an external text file without the new warning from Barry Rowlingson on 2008-10-29 (R devel archive)

Re: [Rd] writting null (\000 or ^@) to an external text file without the new warning

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Tue, 28 Oct 2008 19:57:07 +0000

2008/10/28 Greg Snow <Greg.Snow_at_imail.org>:

> I have some functions that write an external text file for postprocessing by another program.  Some instructions to the other program need to be indicated by null values (\000 or ^@).  The function currently uses code like:
>
> writeChar(rawToChar(as.raw(0)), con)
>
> where con is a connection to the file.  Previous to version 2.8.0 this worked fine.  With 2.8.0 it still works, but I get a warning message about "truncating string with embedded null: '\0'" every time.  This is documented and not a bug, but I still find it annoying.
>
> One thing I could do is to turn off all warnings before doing this, but then if there is some other warning generated, then I will miss the other warning(s).
>
> Is there a better way to write the null to the text file? Or is there a way to suppress just this warning without suppressing any other warnings that may occur?

 The warning happens when you construct the string, so somehow you have to avoid making the string. How about using writeBin with a connection opened in binary mode:

> con=file("test2.raw","wb")
> writeBin(as.raw(1),con,size=1)
> writeBin(as.raw(0),con,size=1)
> writeBin(as.raw(0),con,size=1)
> writeBin(as.raw(7),con,size=1)
> close(con)
>

Save workspace image? [y/n/c]: n

 now lets dump it:

 rowlings_at_fab008000006:~$ od -x test2.raw  0000000 0001 0700
 0000004

 Which I think is correct, maybe some fiddling with endianness is needed...

Barry



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 28 Oct 2008 - 20:00:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 28 Oct 2008 - 20:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive