add saveRandomArray

This commit is contained in:
Kaehvaman 2024-11-01 21:29:18 +04:00
parent 9f8b45ebbe
commit 627d531df2
2 changed files with 40 additions and 19 deletions

View File

@ -1,16 +1,2 @@
15 30
1 5 5 4 4 5 4 0 0 4 2 5 5 1 3 1 5 1 2 3 0 3 0 2 3 4 4 3 2 2 5
2
3
4
5
6
7
8
9
10
11
12
13
14
15

View File

@ -9,8 +9,8 @@ void printarr(int arr[], int len) {
printf("\b\b]\n"); printf("\b\b]\n");
} }
int load(int** parr) { int load(int** parr, char filename[]) {
FILE* file = fopen("data.txt", "r"); FILE* file = fopen(filename, "r");
if (file == NULL) { if (file == NULL) {
puts("Ôàéë íå íàéäåí"); puts("Ôàéë íå íàéäåí");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -29,13 +29,48 @@ int load(int** parr) {
fscanf_s(file, "%d", (*parr + i)); fscanf_s(file, "%d", (*parr + i));
} }
fclose(file);
return len; return len;
} }
void save(int arr[], int len, char filename[]) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
puts("Íå óäàëîñü ñîçäàòü ôàéë");
exit(EXIT_FAILURE);
}
fprintf_s(file, "%d\n", len);
for (int i = 0; i < len; i++) {
fprintf_s(file, "%d ", arr[i]);
}
fclose(file);
}
void saveRandomArray(int len, char filename[]) {
int* parr;
parr = (int*)malloc(sizeof(int) * len);
if (parr == NULL) {
printf_s("Not enough memory to load data\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < len; i++) {
parr[i] = rand() % 6;
}
save(parr, len, filename);
}
int main() { int main() {
int* parr; int* parr;
int len; int len;
len = load(&parr); //saveRandomArray(30, "data.txt");
len = load(&parr, "data.txt");
printarr(parr, len); printarr(parr, len);
return 0; return 0;
} }