www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/17/07:48:30

From: ANTHONY APPLEYARD <A DOT APPLEYARD AT fs1 DOT mt DOT umist DOT ac DOT uk>
To: djgpp AT sun DOT soe DOT clarkson DOT edu (DJGPP users list)
Date: Thu, 17 Nov 1994 08:32:03 GMT
Subject: open a file (was: Re: fseek() trouble ?!?)
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (DJGPP users list)

kunst AT prl DOT philips DOT nl wrote on Wed 16 Nov 1994 17:38:47 +0100 (MET) (Subject:
fseek() trouble ?!?):-
  > FILE *openfile (const char *fname, const char *mode) ...

  Here is an open-a-file C function that I sometimes use:-

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
/*-----*/
int yesno(char*s) {char c,d;
A: printf("%s",s); c=d=getchar(); while(d!='\n') d=getchar();
if(c=='Y'?:c=='y') return 1;
if(c=='N'?:c=='n') return 0; printf("(answer 'yes' or 'no') "); goto A;}
/*-----*/
int fexists(char*n){FILE*F=fopen(n,"r"); if(F) {fclose(F); return 1;} return 0;}
/*-----*/
FILE *myopen(char*s,int readq,int bin=0) {FILE *f; char buff[256]; int i;
AGAIN: printf(s); fgets(buff,256,stdin); if(i=strlen(buff)) buff[i-1]=0;
if(readq) {if(!(f=fopen(buff,bin?"rb":"r"))) {
    printf("file %s does not exist\n",buff); goto AGAIN;} return f;}
else {
    if(fexists(buff)) {
	if(yesno("this file already exists: do you want to append to it?"))
	  f=fopen(buff,bin?"ab":"a");
      else if(yesno("do you want to overwrite it?")) f=fopen(buff,bin?"wb":"w");
	else goto AGAIN;}
    else f=fopen(buff,bin?"wb":"w");
    if(!f) {
	puts("error while opening file, or not legal filename"); goto AGAIN; }
    return f; }}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019