Advertisement
Guest User

Untitled

a guest
Feb 21st, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. #include <locale.h>
  5. #include <io.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8.  
  9. int main() {
  10.  
  11.     char message[] = "hello world";
  12.  
  13.     char filename[] = "aboba.txt";
  14.  
  15.     FILE* fp;
  16.  
  17.     if(fopen_s(&fp, &filename, "w") == 0) {
  18.  
  19.         if (fputs(message, fp) == EOF) {
  20.             perror("WRITE FILE ERROR");
  21.             return 3;
  22.         };
  23.  
  24.         if (fclose(fp) == EOF) {
  25.             perror("CLOSE FILE ERROR");
  26.             return 2;
  27.         }
  28.     }
  29.     else {
  30.         perror("OPEN FILE ERROR");
  31.         return 1;
  32.     }
  33.  
  34.     char buffer[256];
  35.  
  36.     if (fopen_s(&fp, filename, "r") == 0) {
  37.         if (fgets(buffer, 256, fp) == EOF) {
  38.             perror("READ FILE ERROR");
  39.             return 4;
  40.         }
  41.         if (fclose(fp) == EOF) {
  42.             perror("CLOSE FILE ERROR");
  43.             return 2;
  44.         }
  45.     }
  46.     else {
  47.         perror("OPEN FILE ERROR");
  48.         return 1;
  49.     }
  50.  
  51.     printf("%s", buffer);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement