Arrays as files
karl at haddock
karl at haddock
Thu Sep 4 04:27:00 AEST 1986
ihlpa!reb2 (Rick Brown) writes:
>I would like to do the following: 1) Read an input file line
>by line and save each line in an array (using an AWK-like
>interface) 2) Then, within the same program, cause STDIN
>to point to the array so that the program can read from STDIN
>and get each of the saved input lines.
There are some NON-PORTABLE tricks that can make a FILE* use a string rather
than a file descriptor. I won't go into them here.
But, if you really have an array of lines (each of which is an array of char)
then you need more power than this anyway. When you're at the end of a line,
you have to convert your EOL indicator ('\0', '\n', count==0, or whatever)
into a newline for getc() to return, and reset your pointers to get to the
next input line.
I would suggest that you introduce a new type ("gfile", say) for an object
that may be connected to either a string or a stdio FILE. (You could bypass
the stdio FILE and use the system calls, if appropriate.) Make your own
function ("ggetc()") which checks a flag member of its argument to see if it
refers to a FILE (in which case it calls getc()) or an array (in which case
it performs as described above).
Karl W. Z. Heuer (ima!haddock!karl; karl at haddock.isc.com), The Walking Lint
More information about the Comp.lang.c
mailing list