Guest User

Untitled

a guest
Feb 20th, 2025
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  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 = fopen(filename, "w");
  16.  
  17.     if (fp) {
  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.     fp = fopen(filename, "r");
  37.  
  38.     if (fp) {
  39.         if (fgets(buffer, 256, fp) == EOF) {
  40.             perror("READ FILE ERROR");
  41.             return 4;
  42.         }
  43.         if (fclose(fp) == EOF) {
  44.             perror("CLOSE FILE ERROR");
  45.             return 2;
  46.         }
  47.     }
  48.     else {
  49.         perror("OPEN FILE ERROR");
  50.         return 1;
  51.     }
  52.  
  53.     printf("%s", buffer);
  54.  
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment