Michael Steven Rooney
2010-May-05 17:07 UTC
[R] handling nulls while reading mainframe file
Hi, I am trying to read in records from an old main frame file. Each record is 640 bytes. I had some success using the following statement: iconv(readChar(con, nchars=640, useBytes TRUE),from="IBM037",to="",sub="#") What I have found, however, is that ocassionally a record will contain a Null halfway through, which causes readChar() not to read the rest of the record. It is a very old database (from the 1970s I think), and I am not really sure why the Nulls are there. I do know that that after the occurence of the first Null, the record looks normal (it's not Nulls to the 640th byte). In any case, I would like to get the whole record and just put some placeholder, like a 0 or a #, in place of the Nulls. I tried using readBin() instead, but I couldn't figure out how to pass that off to iconv... Can anyone help? Thanks, Mike [[alternative HTML version deleted]]
Try using readBin and 'raw' x <- readBin('yourFile', 'raw', n=1e6) You can then scan 'x' and replace NULLs (I assume they are 0x00) with whatever you want.> str(x)raw [1:10] 00 00 22 00 ...> x[1] 00 00 22 00 00 00 00 00 00 00> x[x == as.raw(0)] <- as.raw(0xab) > x[1] ab ab 22 ab ab ab ab ab ab ab>On Wed, May 5, 2010 at 1:07 PM, Michael Steven Rooney < michael.s.rooney@gmail.com> wrote:> Hi, > > I am trying to read in records from an old main frame file. Each record is > 640 bytes. I had some success using the following statement: > > iconv(readChar(con, nchars=640, useBytes > TRUE),from="IBM037",to="",sub="#") > > What I have found, however, is that ocassionally a record will contain a > Null halfway through, which causes readChar() not to read the rest of the > record. It is a very old database (from the 1970s I think), and I am not > really sure why the Nulls are there. I do know that that after the > occurence > of the first Null, the record looks normal (it's not Nulls to the 640th > byte). In any case, I would like to get the whole record and just put some > placeholder, like a 0 or a #, in place of the Nulls. > > I tried using readBin() instead, but I couldn't figure out how to pass that > off to iconv... > > Can anyone help? > > Thanks, > Mike > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]