[try Beta version]
Not logged in

 
Question about ofstream!!!

Jun 14, 2013 at 9:03pm
Hi I have a question on ofstream!!
I am trying to write program that takes words from a text file and sort then in increasing order by using min priority queue.!!
I am having trouble on outputting sorted sting data.
1
2
3
4
5
6
7
  outfile.open("result.txt");
  string temp;
  for (int i = 0; i<50; ++i){
    temp = pq1.top() + " ";
    outfile<<temp;
  }
  outfile.close();

As you can see I assigned the element from the min priority queue named pq1 to the string variable temp. And then try to write into the txt file named "result.txt". The txt file "result.txt" have weird text instead the words separated by space. Does anyone know why this is happening??
Jun 14, 2013 at 9:20pm
Your code looks fine, except that you are forgetting to pop the top item in the queue on each loop.

I suspect that the data in your pq is corrupted. Are you sure it is declared as

1
2
std::priority_queue <std::string> pq1;

This will sort alphabetically, shorter strings first, case-sensitive. Right?

Hope this helps.
Jun 14, 2013 at 10:46pm
oh thanks..!! I fixed it now!!
Topic archived. No new replies allowed.