FORTRAN type I/O in C - Help!
Richard A. O'Keefe
ok at goanna.cs.rmit.oz.au
Thu Jun 27 17:27:12 AEST 1991
In article <31839 at hydra.gatech.EDU>, griffin at 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 <four decimal digits> 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.
More information about the Comp.lang.c
mailing list