Create load() function

This commit is contained in:
Kaehvaman 2024-11-01 21:07:49 +04:00
parent 8b8abd7da9
commit 9f8b45ebbe
2 changed files with 43 additions and 7 deletions

16
lab19/lab19/data.txt Normal file
View File

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

View File

@ -1,21 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
void load(int arr[]) {
FILE* file = fopen("\\data.txt", "r");
void printarr(int arr[], int len) {
printf("[");
for (int i = 0; i < len; i++) {
printf("%d, ", arr[i]);
}
printf("\b\b]\n");
}
int load(int** parr) {
FILE* file = fopen("data.txt", "r");
if (file == NULL) {
puts("Ôàéë íå íàéäåí");
return;
exit(EXIT_FAILURE);
}
int len;
int len = 1;
fscanf_s(file, "%d", &len);
for (int i = 0; i < len; i++) {
fscanf_s(file, "%d", &arr[i]);
*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++) {
fscanf_s(file, "%d", (*parr + i));
}
return len;
}
int main() {
int* parr;
int len;
len = load(&parr);
printarr(parr, len);
return 0;
}