Help with Tree lists.

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
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <fstream>

#include "bintree.h"
#include "binnode.h"

using namespace std;

void fillTree(bintree<string> &treeRoot);
//void printTreeStats(const bintree<string> &treeRoot);

int main(int argc, char *argv[])
{
    
   const int MAXDATA = 1000; 
     bintree<string> treeRoot;

   /* if (argc != 2) {
      cout << "Syntax : family familyFile\n";
      return 0;
   }*/
 
   fillTree(treeRoot);
 //  printTreeStats(treeRoot);
   treeRoot.rebalance();
//   printTreeStats(treeRoot);

   return 0;
}

void fillTree(bintree<string> &treeRoot)
{
   ifstream fin;
   string c;
   
   fin.open("dict.txt");
   if (!fin) {
      cout << "Unable to read from  file " << "\n";
      exit(0);
   }
   
   while (!fin.eof()) {
         c = fin.get();
         treeRoot.insert(c);
      
   }
   fin.close();   
}


/*void print(const bintree<string> &treeRoot)
{
   
}*/


Hey guys..

This is a small program im trying to write. A spell checker.
Anyways getting to the point. I want to know a method to read from the file and insert 1000 words from that file into a tree list. I have managed to insert one but i am not quite sure how to iterate through the file. Any help would be welcome :)

Thank you.
1
2
3
4
while( fin ){//fin will evaluate to false when an error occurs.
   getline( fin, c );//reads a line.
   //do stuff with c...
}

Your previous code read the file char by char. If your tree didn't reflect that, there might be something wrong with it..

Also, don't post in other threads to ask about your problems.
I would recommend putting the file read into the while() clause:
1
2
3
4
5
while(std::getline(fin, c))
{
    // now we know that getline() succeeded
    treeRoot.insert(c);
}

What you do depends on your dictionary data. The above is good if you have one word per line. If you simply separate your words with spaces then you can use this:
1
2
3
4
5
while(fin >> c)
{
    // now we know that getline() succeeded
    treeRoot.insert(c);
}

Last edited on
sorry about posting in other posts .. i just thought because they had similar problems it might be easier. Anyways thanx for the replies. The thing i dont understand is that how does it know to iterate through the file ? hmm
There is a pointer in the fstream object that marks the position of the next char to read. When getline, .get, >>, etc. is called that pointer is advanced.
thanx works like a charm :)
Topic archived. No new replies allowed.