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