-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
65 lines (55 loc) · 1.52 KB
/
File.cpp
File metadata and controls
65 lines (55 loc) · 1.52 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include "Tree.h"
#include "Strings.h"
#include "File.h"
void skipTrash(FILE*& fp) {
char symb = fgetc(fp);
do {
if (symb == ' ' || symb == '\n' || symb == '\0')
symb = fgetc(fp);
} while (symb == ' ' && symb == '\n' && symb == '\0');
if (!feof(fp)) fseek(fp, -1, SEEK_CUR);
}
bool openFile(FILE*& fp, const char* file_name) {
if (fp == nullptr) {
printf("Can't open file \"%s\"!\n", file_name);
system("CLS");
return false;
}
return true;
}
void initFromFile(FILE* fp, treeNode*& root) {
char* tmp = (char*)malloc(buffSizeStr1024 * sizeof(char));
char* way = (char*)malloc(buffSizeStr1024 * sizeof(char));
do {
fscanf(fp, "%[^*0-9]", tmp);
fscanf(fp, "%s", way);
if (*way == '*') addNode(root, tmp, 0);
else {
treeNode* tempNode = findLeaf(way, root);
addNode(tempNode, tmp, *(way + my_strlen(way) - 1) - '0');
}
skipTrash(fp);
} while (!feof(fp));
free(way);
free(tmp);
}
void rewriteDatabase(FILE* fp, treeNode*& node, bool root/*"true" at 1st request*/) {
if (root) fprintf(fp, "%s*\n", node->data);
if (node->leftLeaf != nullptr) {
fprintf(fp, "%s%s\n", node->leftLeaf->data, node->leftLeaf->key);
rewriteDatabase(fp, node->leftLeaf, false);
}
if (node->rightLeaf != nullptr) {
fprintf(fp, "%s%s\n", node->rightLeaf->data, node->rightLeaf->key);
rewriteDatabase(fp, node->rightLeaf, false);
}
}
void updateList(char* newFile) {
FILE* fp;
fopen_s(&fp, FILES_LIST, "a+");
fprintf(fp, "%s\n", newFile);
fclose(fp);
}