Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!spool.mu.edu!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: FORTRAN type I/O in C - Help! Message-ID: <6535@goanna.cs.rmit.oz.au> Date: 27 Jun 91 07:27:12 GMT Article-I.D.: goanna.6535 References: <31839@hydra.gatech.EDU> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 34 In article <31839@hydra.gatech.EDU>, griffin@prism.gatech.EDU (GRIFFIN,JEFFREY A) writes: > I am looking for suggestions for reading a data file of which two > records are listed below. Each record consists of 512 characters > plus a new-line. There is an initial byte count of 0133 then 32 > integer values, another byte count of 0133 and 32 integers, the > last byte count of 0133 and the last 32 integers. The remainder of > the record is padded with the '^' character. What you have here is ANSI format for a tape with variable-length records stored in 512-byte blocks. The new-line is probably someone trying to be helpful. I suggest attacking this in two stages. Stage 1: break the file into records. int read_Record(FILE *f, char *buffer) /* returns EOF */ { /* or strlen(buffer) */ int c; /* EOF also means "error" */ /* We should be at the beginning of a record. */ /* Padding ^ characters and extra new-lines may intervene. */ do { do c = getc(f); while (c == '^'); } while (c == '\n'); /* By now, only EOF and are ok. */ if (c == EOF) return EOF; /* real EOF; others=error */ ungetc(c, f); if (fscanf(f, "%4d", &c) != 1 || c <= 0) return EOF; if (fread(buffer, 1, c, f) != c) return EOF; buffer[c] = '\0'; return c; } Stage 2: parse the records using sscanf(), or, perhaps easier in this case, strtol(). -- I agree with Jim Giles about many of the deficiencies of present UNIX.