I have a file that contains NUL characters.
This file is generated by another program I have no control over, but I have to read it in order to get some crucial information.
Unfortunately, readChar() truncates the output with this warning:
In readChar("output.txt", 1e+05) : truncating string with embedded nuls
Is there a way around this problem?
By convention, a text file cannot contain non-printable characters (including NUL). If a file contains such characters, it isn’t a text file — it’s a binary file.
R strictly1 adheres to this convention, and completely disallows NUL characters. You really need to read and treat the data as binary data. This means using
readBinand therawdata type:Now we can fix the buffer by removing embedded zero bytes. This assumes UTF-x or ASCII encoding! Other encodings might have embedded zero bytes that need to be interpreted!
1 Maybe too strictly …