Add an word in the file

I am building a tree for algorithm t9, it should read the ordered txt with the most used words, as the code below. However, when a word does not exist in the file I need to add it to the top of the file. I only know the function that adds at the end of the file. How can I do this?

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
void Arvore::ConstrutorArvore() {
	string palavra;
	//ifstream priority("priority.txt");
	ifstream palavrast9("termos1_full_ordenado.txt");
	if (!palavrast9.is_open()) {
		system("cls");
		cout << "Erro ao abrir arquivo" << endl;
	} else
		while (!palavrast9.eof()) {
			palavra.clear();
			palavrast9 >> palavra;
			Elemento *elemento = raiz;

			for (int i = 0; i < palavra.size(); i++) {
				switch (tolower(palavra[i])) {
				case 'a':
				case 'b':
				case 'c':
					if (!elemento->p2)
						elemento->p2 = new Elemento();
					elemento = elemento->p2;
					break;

				case 'd':
				case 'e':
				case 'f':
					if (!elemento->p3)
						elemento->p3 = new Elemento();
					elemento = elemento->p3;
					break;

				case 'g':
				case 'h':
				case 'i':
					if (!elemento->p4)
						elemento->p4 = new Elemento();
					elemento = elemento->p4;
					break;

				case 'j':
				case 'k':
				case 'l':
					if (!elemento->p5)
						elemento->p5 = new Elemento();
					elemento = elemento->p5;
					break;

				case 'm':
				case 'n':
				case 'o':
					if (!elemento->p6)
						elemento->p6 = new Elemento();
					elemento = elemento->p6;
					break;

				case 'p':
				case 'q':
				case 'r':
				case 's':
					if (!elemento->p7)
						elemento->p7 = new Elemento();
					elemento = elemento->p7;
					break;

				case 't':
				case 'u':
				case 'v':
					if (!elemento->p8)
						elemento->p8 = new Elemento();
					elemento = elemento->p8;
					break;

				case 'w':
				case 'x':
				case 'y':
				case 'z':
					if (!elemento->p9)
						elemento->p9 = new Elemento();
					elemento = elemento->p9;
					break;

				default:
					if (!elemento->p1)
						elemento->p1 = new Elemento();
					elemento = elemento->p1;
					break;
				}
				elemento->palavras.push_back(palavra);
			}
		}
	//prioridade.close();
	palavrast9.close();
}
Last edited on
files are like C-arrays. if you want to add at the front, you have to shift what is there down to make room then write to the front. In a small file you do this by reading the file into memory, then overwriting it with newdata+old data. To insert in the middle is similar, you read the whole thing, split it into 2 chunks, and write first, new, second chunks where new is what you insert. Appending can be done without any extra effort. <<--- what that means is that better logic that appends instead of prepends is a better design if you can do it. Its not always possible to do that, as you said the file is ordered so you have to do what you have to do...

Topic archived. No new replies allowed.