Advertisement
Wow_Rasl

Untitled

Jun 3rd, 2022
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <locale.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define NAME "array.bin"
  7.  
  8. void creatingfile(const char* file) {
  9.     FILE* f = fopen(file, "wb");
  10.     if (!f) {
  11.         printf("Не получилось открыть файл\n");
  12.         return;
  13.     }
  14.     rewind(f);
  15.     int n;
  16.     printf("Введите количество элементов(компонент)\n");
  17.     scanf("%d", &n);
  18.     printf("Введите %d элементов:\n", n);
  19.     for (int i = 0; i < n; i++) {
  20.         double cur_num;
  21.         scanf("%lf", &cur_num);
  22.         fwrite(&cur_num, sizeof(double), 1, f);
  23.     }
  24.     fclose(f);
  25. }
  26.  
  27.  
  28. void printingfile(const char* file) {
  29.     FILE* f = fopen(file, "rb");
  30.     if (!f) {
  31.         printf("Не получилось открыть файл\n");
  32.         return;
  33.     }
  34.     printf("Содержимое файла:\n");
  35.     rewind(f);
  36.     while (!feof(f)) {
  37.         double el;
  38.         fread(&el, sizeof(double), 1, f);
  39.         if (!feof(f)) {
  40.             printf("%lf\t", el);
  41.         }
  42.     }
  43.     printf("\n");
  44.     fclose(f);
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50.     setlocale(LC_ALL, "RUS");
  51.     creatingfile(NAME);
  52.     printingfile(NAME);
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement