[R] Reading data from file made by C fwrite

Prof Brian D Ripley ripley at stats.ox.ac.uk
Tue Feb 29 20:46:14 CET 2000


On Tue, 29 Feb 2000, Kjetil Kjernsmo wrote:

> I'm currently working on importing data from files created by a C program
> (that I have not written myself, I have the source code, but I don't know
> if the author wants to publish it yet). 
> 
> I really don't know much about C, but I can understand that the program
> writes to a file using 
> fwrite(MAP,sizeof(float),MAP_i*MAP_j,f)
> where MAP most probably is a pointer to a two-dimensional array of single
> precision floats. This is what I want to read into a matrix in R.

Just link a bit of C in to read the format with fread. You will need to do
this anyway: R does not use the float type.

Here's a bit of code we use for similar purposes. This reads 64x64 slices
of 16 bit values from fMRI machine files.

/* Read a slice from a .img file */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "R.h"

void readimg(unsigned int *ans, int *nslice, char **name)
{
  int fd, i;
  unsigned int t1, t2;
  char *p, buf[64*64*2];
  long nread;
  
  fd = open(name[0], O_RDONLY);
  if(fd < 0) {
    PROBLEM "can't open file" RECOVER(NULL_ENTRY);
  }
  lseek(fd, 2*64*64*(*nslice), SEEK_SET);
  
  nread = read(fd, buf, 64 * 64*2);
  p = buf;
  for(i = 0; i < 64*64; i++) {
    t1 = (*p++)&0xFF;
    t2 = (*p++)&0xFF;
    *ans++ = 256 * t1 + t2;
  }
  close(fd);
}

called by

getslice <- function (nslice, name = "MRI.img") 
.C("readimg", ans = integer(64 * 64), as.integer(nslice), name)$ans


> When I look at the files written by this program, it is clear that they
> are not on a simple form, less says it is a binary file. However, it
> appears to be kind of common, as I have seen other packages (such as IDL)
> reading files like this without much ado... :-) So, can I read the data of
> these files into R? (If not, I may sit down and try to hack it up myself,
> some hints would be greatly appreciated).

Um. However do they know what it is (float, double, integer,
dimensions)....  Yes, they read it, often incorrectly!

Alternatively, netpbm has lots of filters to dump such formats to ASCII.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list