How to extract words from files

I want to know how I can extract words from a text file and store them in a char array. The file will probably have to store hundreds of words.

1
2
3
4
5
6
7
8
  FILE *in;
  in = fopen("Dictionary.txt", "r"); 
	
  char[20] temp;
  while (fscanf(in, " %20s", temp) == 1) {
	gets(temp);
  }
  in.close();


That mess up there is what I've pieced together from google.

Note: I don't want to use c++ stuff, so no strings.
I don't want to use c++ stuff
Ha ha, so why're you trying
in.close();


I assumed you want an array of words, not just one giant array of everything in the file. Below is an example in standard C (C89), maximum 1024 words each up to 19 characters.

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
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORD 20
#define MAX_WORDS 1024

int main(int argc, char *argv[])
{
	char words[MAX_WORDS][MAX_WORD];
	FILE *inf = fopen(argv[1], "r");
	int c = 0;
	int i = 0;
	int word = 0;

	memset(words, 0, MAX_WORDS * MAX_WORD);
	while (((c = fgetc(inf)) != EOF) && (word <= MAX_WORDS))
	{
		int j = 0;
		if (isspace(c)) continue; 
		while (!isspace(c))
		{
			if (j < (MAX_WORD - 1))
			{
				words[word][j] = c;
				++j;
			}
			
			c = fgetc(inf);
			if ((c == EOF) || isspace(c))
			{
				ungetc(c, inf);
				++word;
			}
		}
	}
	
	fclose(inf);
	
	for (i = 0; i < word; ++i)
	{
		fprintf(stdout, "%s\n", words[i]);
	}
	
	return 0;
}
It works just as intented, thanks for that. Problem is, I was trying to integrate it into an existing program and it doesn't work. Beware, it's a big SOB lol.

The original program was a C implementation of a TRIE. I was trying to make it work by reading from a text file rather than manually inputting words. Basically a TRIE dictionary.

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

//--------------------------------------------------------------------------------MAIN.CPP 

#include"header.h"


int main()
{
	TrieNode *trie;
	char UserInputWord[20], cont_insert = ' ';
	int option = 0;
	trie = NULL;
label_menu:
	while (option != 5)
	{
		cout << endl << " Menu: " << endl;
		cout << "___________________________________________" << endl;
		cout << " 1. Create tree\n 2. Insert node\n 3. Search for node\n 4. Display tree\n 5. Exit\n";
		cout << "\n\n\nInput choice: ";
		cin >> option;
		switch (option)
		{
		case 1:
			while (cont_insert != 'n')
			{
				//readDictionary(trie);

				cout << endl << "Insert word :";
				cin >> UserInputWord;
				trie = Insert(trie, UserInputWord);
				cout << "\n Continue ? <y/n>";
				cont_insert = _getch();
			}
			break;
		case 2:
			cout << endl << "Insert word :";
			cin >> UserInputWord;
			Insert(trie, UserInputWord);
			break;
		case 3:
			cout << endl << "Searched word :";
			cin >> UserInputWord;
			Find(trie, UserInputWord);
			break;
		case 4:
			DisplayTrie(trie, 0);
			break;
		case 5:
			break;
		default:
			cout << "Choose from the displayed options (1-5) !!!";
			goto label_menu;
		}
	}
	system("PAUSE");
	return 0;
}


//-------------------------------------------------------------------------HEADER.H

#define MAX_WORD 20
#define MAX_WORDS 1024

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <conio.h>
#include <iostream>

using namespace std;



#define NR 27 

typedef struct TrieNode{
	bool NotLeaf;
	TrieNode *pChildren[NR];
	char word[20];
};

TrieNode *NewLeaf(char keyWord[20]);
TrieNode *NewIntern();
//void readDictionary(TrieNode *trie);
void Find(TrieNode *trie, char keyWord[20]);
TrieNode *Insert(TrieNode *trie, char keyWord[20]);
void DisplayTrie(TrieNode *trie, int nivel);



//---------------------------------------------------------------------FUNCTIONS.CPP

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS

#include"header.h"



TrieNode *NewLeaf(char keyWord[20])
{
	TrieNode *node;
	int count;

	node = (TrieNode *)malloc(sizeof(TrieNode));
	for (count = 0; count < 27; count++)
		node->pChildren[count] = NULL;
	node->NotLeaf = false;
	strcpy_s(node->word, keyWord);
	return node;
}

TrieNode *NewIntern()
{
	TrieNode *node;
	int count;

	node = (TrieNode *)malloc(sizeof(TrieNode));
	for (count = 0; count < 27; count++)
		node->pChildren[count] = NULL;
	node->NotLeaf = true;
	node->word[0] = 0;
	return node;
}

/*void readDictionary(TrieNode *trie)
{
	char words[MAX_WORDS][MAX_WORD];
	FILE *inf = fopen("Dictionary.txt", "r");
	int c = 0;
	int i = 0;
	int word = 0;

	memset(words, 0, MAX_WORDS * MAX_WORD);
	while (((c = fgetc(inf)) != EOF) && (word <= MAX_WORDS))
	{
		int j = 0;
		if (isspace(c)) continue;
		while (!isspace(c))
		{
			if (j < (MAX_WORD - 1))
			{
				words[word][j] = c;
				++j;
			}

			c = fgetc(inf);
			if ((c == EOF) || isspace(c))
			{
				ungetc(c, inf);
				++word;
			}
		}
	}

	fclose(inf);

	for (i = 0; i < word; ++i)
	{
		Insert(trie, words[i]);
	}
}*/

void Find(TrieNode *trie, char keyWord[20])
{
	TrieNode *next, *index, *data;
	int count;
	next = trie;
	if (next == NULL)
	{
		cout << "Word not found in trie !!!!" << endl;
		exit(1);
	}
	else
		index = next;
	count = 0;
	while ((index->NotLeaf == true) && (count < strlen(keyWord)) && (index->pChildren[keyWord[count] - 'a'] != NULL))
	{
		next = index->pChildren[keyWord[count] - 'a'];
		index = next;
		count++;
	}
	if (next == NULL)
		cout << "Word not found in trie !!!!" << endl;
	else
	{
		data = next;

		if (!strcmp(data->word, keyWord))
			cout << "Key exists --- Word found in trie !!!!" << endl;
		else
			if ((data->pChildren[26]) && !strcmp(data->pChildren[26]->word, keyWord))
				cout << "Key exists --- Word found in trie !!!!" << endl;
			else
				cout << "Word not found in trie !!!!" << endl;
	}
}

TrieNode *Insert(TrieNode *trie, char keyWord[20])
{
	TrieNode *next, *index, *parent = nullptr;
	TrieNode *new_leaf, *data, *new_index;
	TrieNode *oldChildren = nullptr, *newWord = nullptr, *intern;
	int inWordIndex, prefixLenght, lenght = strlen(keyWord);
	next = trie;
	if (next == NULL)
	{
		trie = NewIntern();
		new_leaf = NewLeaf(keyWord);
		trie->pChildren[keyWord[0] - 'a'] = new_leaf;
		return trie;
	}
	else
		index = next;
	inWordIndex = 0;
	while ((inWordIndex < lenght) && (index->NotLeaf == true) && (index->pChildren[keyWord[inWordIndex] - 'a'] != NULL))
	{
		parent = next;
		next = index->pChildren[keyWord[inWordIndex] - 'a'];
		index = next;
		inWordIndex++;
	}

	if ((inWordIndex < lenght) && (index->pChildren[keyWord[inWordIndex] - 'a'] == NULL) && (index->NotLeaf == true))
	{
		new_index = NewLeaf(keyWord);
		index->pChildren[keyWord[inWordIndex] - 'a'] = new_index;
		return trie;
	}
	else
		data = next;
	if (!strcmp(data->word, keyWord))
		cout << "Word already exists in trie !!!" << endl;
	else
	{
		oldChildren = parent->pChildren[keyWord[inWordIndex - 1] - 'a'];
		newWord = NewLeaf(keyWord);
		prefixLenght = strlen(keyWord);
		if (data->word[0] != '\0')
			if (strlen(data->word) < prefixLenght)
				prefixLenght = strlen(data->word);
	}
	bool createIntern = false;

	while ((inWordIndex <= prefixLenght) && (((data->word[0] != '\0') && (data->word[inWordIndex - 1] == keyWord[inWordIndex - 1])) || (data->word[0] == '\0')))
	{
		intern = NewIntern();
		parent->pChildren[keyWord[inWordIndex - 1] - 'a'] = intern;
		parent->NotLeaf = true;
		parent = intern;
		inWordIndex++;
		createIntern = true;
	}
	if (createIntern)
		inWordIndex--;

	if ((inWordIndex != prefixLenght) || ((inWordIndex == prefixLenght) && (strlen(keyWord) == strlen(data->word))))
	{
		parent->pChildren[data->word[inWordIndex] - 'a'] = oldChildren;
		parent->pChildren[keyWord[inWordIndex] - 'a'] = newWord;
	}
	else
		if (data->word[0] != '\0')
			if (strlen(data->word) <= prefixLenght)
			{
				parent->pChildren[26] = oldChildren;
				parent->pChildren[keyWord[prefixLenght] - 'a'] = newWord;
			}
			else
			{
				parent->pChildren[26] = newWord;
				parent->pChildren[data->word[prefixLenght] - 'a'] = oldChildren;
			}
		else
		{
			for (int count = 0; count < 27; count++)
				parent->pChildren[count] = oldChildren->pChildren[count];
			parent->pChildren[26] = newWord;
		}
	return trie;
}

void DisplayTrie(TrieNode *trie, int nivel)
{
	int count;
	if (trie)
	{
		if (trie->NotLeaf != true)
		{
			for (count = 0; count <= nivel; count++)
				cout << " ";
			cout << trie->word << endl;
		}
		for (count = 26; count >= 0; count--)
			DisplayTrie(trie->pChildren[count], nivel + 4);
	}
}

#endif 



I didn't write this implementation, but I HAVE to use it, that's why I said no C++. The implementation works manually. I just need it to work by reading from a text file instead of inserting words on the keyboard.

The commented parts are what I was trying with your input. The "1. Create tree" option works. But when I try searching for the words, the console window instantly closes, even with system pause and cin.get. I don't get it...
I realize this looks big, but most of it doesn't need to change. Just the part that reads from the file. I would appreciate it if someone told me what's wrong with it.
Hi Hornet, I'm not sure I understand:
Do you mean that you're still having problems reading from the file?
Can you narrow it down to the sequence of function calls and arguments and the error message you get?
Are you sure your program can find the file? If you're running from an ide, like eclipse or visual studio, the binary you're executing may not be where you think it is, relative to the file you're trying to open - have you tried using an absolute path name?
Also, if you start a command prompt yourself and execute the program, then there'll be no disappearing console, so any diagnostic messages will still be available.
Okay so I did it eventually with a simple function. Just this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
TrieNode *readDictionary(TrieNode *trie)
{
	char words[MAX_WORDS];
	FILE *inf = fopen("Dictionary.txt", "r");
	while (!feof(inf))
	{
		fgets(words, MAX_WORDS, inf);
		words[strlen(words) - 1] = NULL;
		trie = Insert(trie, words);
	}
	fclose(inf);

	return trie;
}


As for the exe, that also closed immediately. Don't know why.
Topic archived. No new replies allowed.