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"; 

So you came up with the above code to read a file but you dont know how to output the resaults to another file?

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?

help yourself.

ofstream fw("foo.txt");
fw<<"write me "<<endl;
fw.close();

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.

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"; 
                        }

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?

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.

Post the code that you have now

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

you need to move test.close() from line 9 to line 12 and test2.close() from line 10 to line 13

How would I lower case all the words?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.