Advertisement
J3st3rs_j0k3

pr7_3_bin

Dec 16th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <locale.h>
  6. #include <math.h>
  7.  
  8.  
  9. void print_file(FILE *);
  10.  
  11. int main()
  12. {
  13.     setlocale(0, "");
  14.  
  15.     FILE *f;
  16.     int size = sizeof(double), offset = 8*size, end_file, flag = 0, flag2 = 0, count = 0;
  17.     double first, last, x, temp;
  18.     if((f = fopen("int.dat", "rb+")) == NULL){
  19.         perror("Ошибка открытия файла");
  20.         return 1;
  21.     }
  22.  
  23.     printf("Исходный файл:\n");
  24.     print_file(f);
  25.  
  26.     printf("\n");
  27.  
  28.     rewind(f);
  29.     while(fread(&x, sizeof(double), 1, f)){
  30.         if (x < 0){
  31.             if(!flag2){
  32.                 first = x;
  33.             }
  34.             last = x;
  35.             count++;
  36.         }
  37.     }
  38.  
  39.     printf("\nПервое отрицательное: %.3lf\n", first);
  40.     printf("Последнее отрицательное: %.3lf\n", last);
  41.  
  42.     rewind(f);
  43.  
  44.     while(fread(&x, sizeof(double), 1, f )){
  45.         if(x == first){
  46.             fseek(f, -sizeof(double), SEEK_CUR);
  47.             fwrite(&last, sizeof(double), 1, f);
  48.         }
  49.         else if (x == last){
  50.             fseek(f, -sizeof(double), SEEK_CUR);
  51.             fwrite(&first, sizeof(double), 1, f);
  52.         }
  53.     }
  54.  
  55.     printf("\nИзмененный файл:\n");
  56.  
  57.     fseek(f, sizeof(double), SEEK_END);
  58.     fwrite(&count, sizeof(double), 1, f); //записываем кол-во отриц. компонет в конец файла
  59.     print_file(f);
  60.     printf("\n");
  61.     fclose(f);
  62.     return 0;
  63. }
  64.  
  65. void print_file(FILE *f){ //печать содержимого файла
  66.     double x, sz = sizeof(double);
  67.     rewind(f);
  68.     while(fread(&x, sz, 1, f)) printf("%.3lf ", x);
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement