Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. typedef struct {
  2.     FILE *handle;
  3.     const char *fname;
  4. } mystruct;
  5.  
  6. mystruct *open_file(const char *file)
  7. {
  8.     mystruct *r = NULL;
  9.     const char *fname = NULL;
  10.     FILE *handle = NULL;
  11.  
  12.     r = malloc(sizeof(r));
  13.     if (r == NULL) goto life_sucks;
  14.  
  15.     handle = fopen(file, "r");
  16.     if (handle == NULL) goto life_sucks;
  17.  
  18.     fname = strdup(file);
  19.     if (fname == NULL) goto life_sucks;
  20.  
  21.     r.handle = handle;
  22.     r.fname = fname;
  23.  
  24.     return r;
  25.  
  26. life_sucks:
  27.     free(fname);
  28.     if (handle != NULL) fclose(handle);
  29.     free(r);
  30.    
  31.     return NULL
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement