[R] End of File for binary files
Duncan Murdoch
murdoch at stats.uwo.ca
Sun Jan 24 20:56:42 CET 2010
On 24/01/2010 2:12 PM, jim holtman wrote:
> If you are using readBin and want to read binary (and not 'character')
> data, then use 'raw' which will return a value if it encounters a
> binary zero and not think it is the end of a character string.
I'm not sure I understand what you mean by reading binary data, but you
should also think about using readChar if you don't intend to read
null-terminated strings.
Duncan Murdoch
It is
> easy enough to setup a test file and try out a couple of the options
> to see what happen. Not enough information was presented in the
> original posting to conclude what was really wanted. Does the binary
> file consist of just bytes, it is integer (32 or 64 bit), is it
> floating point, etc. Once you understand the structure of the data,
> is it not difficult to read in the data. Binary and 'character' are
> not necessarily the same information when trying to read from a file.
>
> On Sun, Jan 24, 2010 at 12:15 PM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
>> On Sun, Jan 24, 2010 at 3:56 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>>> On 23/01/2010 10:40 PM, rn00b wrote:
>>>> I am using readBin to continuously read characters from the binary file.
>>>> I'm
>>>> trying to figure out how many characters are in the file. What I would
>>>> like
>>>> to do is something like
>>>> (while! EOF)
>>>> {
>>>> charRead <-.Internal(readBin(con,"character",1L,NA,TRUE,swap))
>>>> i++
>>>> }
>>>>
>>>> I'm not clear on how to determine the EOF condition in this case.
>>> You should not be calling .Internal. It's for internal use, subject to
>>> change, etc.
>>>
>>> Using readBin(...) you can detect EOF by reading fewer than n items when
>>> you ask for n.
>> Is this safe? Is EOF the only case where readBin() returns fewer
>> elements than you requested? Does it depend on the type of connection
>> you are reading from? Does it depend on OS?
>>
>> The help("readBin") does not say much about this, but it says:
>>
>> "If readBin(what = character()) is used incorrectly on a file which
>> does not contain C-style character strings, warnings (usually many)
>> are given. From a file or connection, the input will be broken into
>> pieces of length 10000 with any final part being discarded."
>>
>> which seems to suggest that you (at least in special cases) can get
>> fewer items than requested without hitting EOF. EOF behavior might be
>> document elsewhere in R.
>>
>> /Henrik
>>
>>> So the loop would be something like
>>>
>>> while (length(charRead <- readBin(con, "character")) > 0) {
>>> i <- i + 1
>>> }
>>>
>>> Duncan Murdoch
>>>
>>> ______________________________________________
>>> R-help at 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
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>> ______________________________________________
>> R-help at 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
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
More information about the R-help
mailing list