compressed files
Tom Stockfisch
tps at chem.ucsd.edu
Fri Jan 19 09:46:33 AEST 1990
In article <3339 at uceng.UC.EDU> trohling at uceng.UC.EDU (tom rohling) writes:
> Can a compressed file be accessed through fortran (or C) much in the
>same way that 'zcat' uncompresses the file to std out but leaves the file
>in its compressed state? i.e. can I read the contents of a compressed file
>from a program without having to uncompress it first? Sort of like zcat
>it into ram where my program can get at it without creating file out
>of it and taking up all that space.
> We have these rather large files (20 Meg uncompressed) we are using
>... and alot of the time there isn't enough room on
>the disk to uncompress all the files and run the program for a while and
>still leave enough disk space for other users.
Use the following routine in place of fopen( "bigFile", "r" ):
/* zopen():
* open a compressed file for reading, filtering it thru zcat.
*/
# include <stdio.h>
static void defaultErrHndlr();
void (*zopenErrHndlr)() = defaultErrHndlr;
FILE *
zopen(name)
char *name;
{
FILE *stream;
int piped[2];
# define READ 0
# define WRITE 1
if ( pipe(piped) == -1 )
(*zopenErrHndlr)( "pipe failure\n" );
switch ( fork() )
{
case -1:
(*zopenErrHndlr)( "fork failure\n" );
case 0: /* child */
close( piped[READ] );
close(1);
if ( dup( piped[WRITE] ) != 1 )
(*zopenErrHndlr)( "dup screwup\n" );
close( piped[WRITE] );
execlp( "zcat", "zcat", name, (char *)0 );
(*zopenErrHndlr)( "cannot start zcat" );
default: /* parent */
close( piped[WRITE] );
stream = fdopen( piped[READ], "r" );
if (stream == NULL)
(*zopenErrHndlr)( "cannot open pipe\n" );
break;
}
return stream;
}
static void
defaultErrHndlr(diagnostic)
char *diagnostic;
{
fprintf( stderr, "zopen(): %s\n", diagnostic );
exit(1);
}
--
|| Tom Stockfisch, UCSD Chemistry tps at chem.ucsd.edu
More information about the Comp.sys.sgi
mailing list