Для создания бинарного файла include malloc include stdio include stdl

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
Для создания бинарного файла:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int randomno()
{
return rand()%25 - rand()%30;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "ru");
char path[250];
char path_dec[250];
if (argc == 1)
{
printf("Enter file path:");
gets(path);
}
else
{
strcpy(path, argv[1]);
}
strcpy(path_dec, path);
strcat(path, "\\file.bin");
strcat(path_dec,"\\file_dec.txt");
FILE* file = fopen(path, "wb+");
FILE* file_dec = fopen(path_dec, "wt+");
int N;
printf("Vvedite kol-vo chisel:");
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
int tmp = randomno();
fprintf(file_dec, "%3d ", tmp);
fwrite(&tmp, sizeof(int), 1, file);
}
fclose(file);
fclose(file_dec);
return 0;
}
Основной код:
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "ru");
char path[250];
if (argc == 1)
{
printf("Enter file path:");
gets(path);
}
else
{
strcpy(path, argv[1]);
}
FILE* file = fopen(path, "rb+");
fseek(file, 0, SEEK_END);
int n = ftell(file)/sizeof(int);
int* arr;
arr = calloc(n, sizeof(int));
rewind(file);
fread(arr, sizeof(int), n, file);
int i_fp, i_lp, k;
i_fp = i_lp = k = 0;
for (int i = 0; i < n; i++)
{
if (k == 0 && (*(arr + i) > 0))
{
i_fp = i;
k++;
}
if (k == 1 && (*(arr + i) > 0))
{
i_lp = i;
}
}
fclose(file);
remove(path);
int* array;
int tmp_size = n - (i_lp - i_fp) + 1;
array = calloc(tmp_size, sizeof(int));
file = fopen(path, "wb+");
int j = 0;
for (int i = 0; i < n; i++)
{
*(array + j) = *(arr + i);
if (i == i_fp)
{
i = i_lp-1;
}
j++;
}
for (int i = 0; i < n; i++)
{
printf("%3d ", *(arr + i));
}
printf("\n\n");
for (int i = 0; i < tmp_size; i++)
{
printf("%3d ", *(array + i));
}
fwrite(array, sizeof(int), tmp_size, file);
free(arr);
free(array);
fclose(file);
return 0;
}