/******************************************************************** --------------------------------------------------------------------- MODUL : $RCSfile: openfile.c,v $ TASK : Opens a file. If the file name is not specified, the user will be asked for it. In case of a wrong user input it can betried again until WDH times. A pointer to the opend file will be returned. INPUT : infostr - Short description of the file file - file name ( Attention! Length of file name is limited ) mode - Handling mode for the file (r , w) dialog - Flag: 1 for interactive work, 0 otherwise OUTPUT : Pointer to the opened file ( NULL at error ) GLOBAL : Possible failures will be stored in the global variable errno. AUTHOR : Dr. Vinzenz Brendler Research Center Rossendorf Inc., Institute of Radiochemistry VERSION : $Revision: 1.1 $ $Date: 1996/09/17 07:56:58 $ --------------------------------------------------------------------- ********************************************************************/ #include #include #include #define WDH 2 /* Number of user input retrials */ #define PROMPT1 "Name of" #define PROMPT2 "?? Please try again for" extern int errno; FILE *openfile(infostr, file, mode, dialog) char infostr[]; /* File descriptor */ char file[]; /* File name */ char mode[]; /* Access mode */ short int dialog; /* Interactive ? */ { FILE *filepointer; char filename[BUFSIZ]; if(strlen(file) < BUFSIZ) strcpy(filename, file); else return(NULL); if(dialog) dialog += WDH; if(dialog && !strlen(filename)) { fprintf(stderr, "%s %s : ", PROMPT1, infostr); if(gets(filename) == NULL) return(NULL); } do { if(dialog) dialog--; errno = 0; if((filepointer = fopen(filename, mode)) == NULL) { perror(filename); if(dialog) { fprintf(stderr, "%s %s : ", PROMPT2, infostr); if(gets(filename) == NULL) return(NULL); } } } while(dialog && errno); return(filepointer); }