/* /////////////////////////////////////////////////////////////// // // // open.c - gets a stream definition from user // // ver 1.0 // // definitions // // // // Language: Borland C, ver 3.0 // // Platform: Gateway 386/25, win95.400 // // Application: CSE784 demonstration // // Author: Jim Fawcett, 123 Link Hall, Syracuse Univ. // // (315) 443-2654, jfawcett@cat.syr.edu // // // /////////////////////////////////////////////////////////////// */ #include /* free() */ #include /* strcmp() */ #include "lines.h" /* getLine() */ #include "open.h" /*----< open a stream for reading >---------------------------- Inputs: - name of file to open - if null then user response from keyboard Outputs: - returns a FILE pointer to an open stream if file creation is successful, otherwise returns 0 */ FILE *getRStream(char *name) { FILE *fptr; if(!name) { fprintf(stdout," please enter a filename for input: "); name = getLine(stdin); if(!(fptr=fopen(name,"r"))) fprintf(stdout, " can't open file %s\n", name); free(name); return fptr; } if(!strcmp(name,"stdin")) return stdin; if(!(fptr=fopen(name,"r"))) fprintf(stdout, " can't open file %s\n", name); return fptr; } /* */ /*----< open a stream for writing >---------------------------- Inputs: - name of file to open - if null then user response from keyboard Outputs: - returns a FILE pointer to an open stream if file creation is successful, otherwise returns 0 */ FILE *getWStream(char *name) { FILE *fptr; if(!name) { fprintf(stdout, " please enter a filename for output: "); name = getLine(stdin); if(!(fptr=fopen(name,"w"))) fprintf(stdout, " can't open file %s\n", name); free(name); return fptr; } if(!strcmp(name,"stdout")) return stdout; if(!(fptr=fopen(name,"w"))) fprintf(stdout, " can't open file %s\n", name); return fptr; } /*----< test stub >------------------------------------------*/ #ifdef TEST_OPEN #include "lines.c" /* don't include implementation files */ /* except in test stubs: */ /* including avoids need for project */ void main() { FILE *in = getRStream(0); FILE *out = getWStream(0); char *line = getLine(in); fprintf(out, "%s\n", line); fprintf(stdout, "just wrote %s to output file\n", line); } #endif