Re: Filestream problems
In article
<fd3a6fb3-a114-41a8-9219-dc7efbeb3331@m44g2000hsc.googlegroups.com>,
Hani Sharabash <HaniBash@gmail.com> wrote:
On Apr 17, 3:56 am, Carl Barron <cbarron...@adelphia.net> wrote:
In article
<73c37290-e418-4d12-934a-c33075210...@m36g2000hse.googlegroups.com>,
Hani Sharabash <HaniB...@gmail.com> wrote:
I'm writing a program in which I read text from a file, store all of
the words into a binary search tree, and then print the words out to
the screen in order from that tree.
For some reason, opening and using an input filestream is causing
problems for my binary tree functions. When I try to use them
together, my program crashes at runtime. For example:
\
first I would make sure where the error is in this case I would
imput the data and store it in a stanard sorted assoc. container
say std::set<std::string> and print the results from
std::set<std::string> before I blame <fstream> probably there is
a bug in your binary tree code. But if reading it into an
std::set<string> works then its not <fstream>.
I don't understand. Could you clarify a little more?
std::set<std::string> will hold the sorted words with persumably
working code as std::set is 10 years old approx. and should be free
of internal errors.
if you read from <fstream> as you currently do but store the words in
an std::set<std::string> dups are removed , if you want dups then
multi_set<std::string> will work as well.
#include <fstream>
#include <set>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::ifstream in_file(...);
std::set<std::string> words;
while(in_file)
{
std::string this_word;
// your reading code that reads one word into this_word.
words.insert(this_word);
}
// print out results:
std::copy(words.begin(),words.end(),std::ostream_iterator<std::string>
(std::cout,"\n"));
}
now the only possible problem [unless this is an ancient compiler] is in
the reading code. further if this provides what you really want its
done,
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]