[R] read file with readBin (the file was saved with a C-routine)

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jun 28 18:43:11 CEST 2006


On Wed, 28 Jun 2006, Jörn Schulz wrote:

> Hello!
>
> I have problems with using of "readBin" to read files, which was written in C with "fwrite". In the C-File there is the following Code:
>
> fwrite(MyitINI,sizeof(itINItype),1,outfile);
>
> where MyitINI is a structure of the following form
>
> typedef struct{
> int  KernelFileSave;      /* Determined, if Systemmatrix saved or not.*/
> char KernelFileName[200]; /* A-Matrix name                            */
> char StartFileName[200];  /* Startguess (optional)                    */
> int XSamples;             /* No of samples on 1. axis of recon image  */
> int YSamples;             /* No of samples on 2. axis of recon image  */
> float DeltaX;             /* Sampling distance 1. axis of recon image */
> float DeltaY;             /* Sampling distance 2. axis of recon image */
> float Xmin;               /* 1. sample position 1.axis of recon image */
> float Ymin;               /* 1. sample position 2.axis of recon image */
> }  itINItype;
>
> I thought the following use of "readBin" sould it do, but it doesn't
>
> KernelFileSave <- readBin( con, integer(), n=1, size=4 )
> KernelFileName <- readBin( con, character(), n=1 )
> StartFileName <- readBin( con, character(), n=1 )
> XSamples <- readBin( con, integer(), n=1, size=4 )
> YSamples <- readBin( con, integer(), n=1, size=4 )
> DeltaX <- readBin( con, numeric(), n=1, size=4 )
> DeltaY <- readBin( con, numeric(), n=1, size=4 )
> Xmin <- readBin( con, numeric(), n=1, size=4 )
> Ymin <- readBin( con, numeric(), n=1, size=4 )
>
> I think there is a problem if I read the character. Have you any ideas ???

The help page points you to readChar, which seems more appropriate: you 
could also use raw().

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list