You want to read the file as binary then you can substitute the NUL
s, e.g. to replace them by spaces:
r = readBin("00staff.dat", raw(), file.info("00staff.dat")$size)
r[r==as.raw(0)] = as.raw(0x20) ## replace with 0x20 = <space>
writeBin(r, "00staff.txt")
str(readLines("00staff.txt"))
# chr [1:155432] "000540952Anderson Shelley J FW1949 2000R000000000000119460007620 3 0007000704002097907KGKG1616"| __truncated__ ...
You could also substitute the NUL
s with a really rare character (such as "1"
) and work on the string in place, e.g., let's say if you want to replace two NUL
s ("00"
) with one space:
r = readBin("00staff.dat", raw(), file.info("00staff.dat")$size)
r[r==as.raw(0)] = as.raw(1)
a = gsub("11", " ", rawToChar(r), fixed=TRUE)
s = strsplit(a, "
", TRUE)[[1]]
str(s)
# chr [1:155432] "000540952Anderson Shelley J FW1949 2000R000000000000119460007620 3 0007000704002097907KGKG1616"| __truncated__
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…