Why is this only saving one line?

I am loading a text file......... trying to save the results into another text file but it is only saving one line to the new text file. There are hundreds of lines in that need to be save to new text file, any clues?


std::ifstream test1("test1.txt");
std::ofstream test2("test2.txt");
std::istream_iterator<std::string> eof;
std::multiset<std::string> words( std::istream_iterator<std::string>(test1) , eof);
for( std::multiset<std::string>::iterator i = words.begin(); i!=words.end(); i = words.upper_bound(*i) )
{
std::cout << *i<< ": " << words.count(*i) << " times\n";
test2 << *i<< ": " << words.count(*i) << " times\n" << endl;
test1.close();
test2.close();
Last edited on
closed account (Dy7SLyTq)
why not just do while(getline(ifstream_object, string_object)) and then write it to the new file using ofstream_object<< string_object << std::endl?
How would I do that?

I am doing this cos I have to count word occurences and also parse, with this, it parse and shows word occurences.
Last edited on
A useful thing when you want help with a code snippet is to provide a code snippet that compiles and reproduces the problem you are experiencing. The snippet you provided does not do the first, so it cannot do the second.

If test2.close() is in the body of the for loop as it appears to be in your unformatted code above, that's going to be a problem, as any output done in iterations of the loop subsequent to the first will be trying to output to a closed stream.
Topic archived. No new replies allowed.