Advertisement
userxbw

Struct open binary file C

Sep 8th, 2022 (edited)
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct source_files{
  4. FILE *fpt;
  5. char *path;
  6. int stat;
  7. };
  8. void open_struct_file(
  9. struct source_files *da){
  10. printf("in fun %s\n",da->path);
  11. da->fpt=fopen(da->path,"ab+");
  12. if(!da->fpt)
  13. da->stat=0; // false
  14. else
  15. da->stat=1; // true
  16. }
  17. int main(int argc, char **argv){
  18. struct source_files sf;
  19. char *path;
  20. if(argc==1){
  21. sf.path="data.dat";
  22. open_struct_file(&sf);
  23.     if(sf.stat){
  24.         printf("file open %d\n",sf.stat);
  25.     }else{
  26.         printf("file not open %d\n",sf.stat);
  27.         exit(1);
  28.     }
  29. }
  30. else if (argc==2){
  31. path=strdup(argv[1]);
  32. //printf("%s\n",path);
  33. FILE *ff = fopen(path,"ab+");
  34.     if(!ff){
  35.         printf("file not open\n");
  36.         exit(1);
  37.     }else{
  38.         printf("file open\n");
  39.     }
  40. }
  41. return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement