This function reads a text file and shows how many times a words appears in the text file, how do I save it to a different text file after running this function?

                    std::ifstream f("test.txt"); 
                    std::istream_iterator<std::string> eof; 
                    std::multiset<std::string> words( std::istream_iterator<std::string>(f) , 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"; 

No, this is a code I'm trying to learn how to use it. Anybody help on the part where how would you save this function to a text file?

I've tried,

ofstream fw("foo.txt");
fw<< *it << words.count(*it)<<endl;
fw.close();

But "*it" is identified.

You are on the right path. Do you need the results shown to the screen and writtent to the file or do you just need then written to the file? If you just want to write the data to a file then you need to modify the code in the for loop that displays the data to the screen to instead write it to a file. To that you need and ofstream object. Then instead of using std::cout you would use the ofstream object you created.

As a side not have you used any of the classes you have in this code in your class? Can you use the STL? The code you have is somewhat advanced and if I was teaching an intro class and someone handed in what you have I would think they just copied that from the internet somewhere.

Edited 3 Years Ago by NathanOliver

Yes, i need to print to screen and then save to a text file after printing to screen, can you help me?

Well all you need to do is inside your for loop copy the line of output to the screen and change std::cout to whatever ofstream object you create. Don't forget to add curly braces to your for loop since it will become a multiline for loop.

Its not doing anything?

std::ifstream f("test.txt"); 
                        std::istream_iterator<std::string> eof; 
                        std::multiset<std::string> words( std::istream_iterator<std::string>(f) , eof); 
                        for( std::multiset<std::string>::iterator i = words.begin(); i!=words.end(); i = words.upper_bound(*i) )
                        {
                        ofstream fw("foo.txt");
                        fw<< *i<< ": " << words.count(*i) << " times\n" << endl;
                        fw.close();
                        std::cout << *i<< ": " << words.count(*i) << " times\n"; 
                        }

Edited 3 Years Ago by VUEKID

Move line 6 to be in between lines 1 and 2. Move line 8 to be after line 10. You cant open and close the file while you are trying to write to it. Open the file once and close the file once.

ok...it only saved one line into the new text2, why is that?

Edited 3 Years Ago by VUEKID

you have to look carefully into cpp input output stream and files. When you open a file, there are constants that you need to include in the open comand.
1. To append,
2.to truncat
3. open as a binary
etc.... or all of the following

I think it is like this ofstream out("foo.txt",ios::app);
check it up ok?

That does not work, the code above works but file is saving, but only saving one line and not the whole hundgred of lines it is supp0ose to be saving.

std::ifstream test("test.txt"); 
                    std::ofstream test2("test2.txt");
                    std::istream_iterator<std::string> eof; 
                    std::multiset<std::string> words( std::istream_iterator<std::string>(test) , eof); 
                    for( std::multiset<std::string>::iterator i = words.begin(); i!=words.end(); i = words.upper_bound(*i) ) 
                    {
                        std::cout << *i<< ": " << words.count(*i) << " timesn"; 
                        test2 << *i<< ": " << words.count(*i) << " timesn" << endl;
                        test.close();
                        test2.close();
                    }



output:"Defects, ": 1 times
        ....
        ....
        ..
        ....
        100 other stuff....

what is saved to text2:

"Defects,": 1 times

Edited 3 Years Ago by VUEKID

This article has been dead for over six months. Start a new discussion instead.