Incorrect output file: c++ (i/o) question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Incorrect output file: c++ (i/o) question

 
0
  #1
Nov 24th, 2008
I encode a text file using a huffman tree. Which print out to the screen and and output to a file. File only contains the last line that prints to the console.

  1. void traverse(string code = "") const {
  2. string outputFile = "huffman.txt";
  3. ofstream outfile(outputFile.c_str());
  4.  
  5.  
  6. if (child0 !=NULL) {
  7.  
  8. static_cast<huffman_item<dtype> >( *child0).traverse(code + '0' );
  9.  
  10. static_cast<huffman_item<dtype> >( *child1).traverse(code + '1' );
  11.  
  12. } else{
  13.  
  14. outfile << data << freq << code << endl;
  15. cout << " Data: " << data << " Frequency: " << freq << " Code: " << code << endl;
  16.  
  17. }
  18.  
  19. }

This is method. Can someone please help me!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Incorrect output file: c++ (i/o) question

 
0
  #2
Nov 24th, 2008
Is recursion really necessary for this? Whenever possible avoid using it. It's inefficient, hard to implement, and hard to debug. It seems as though the function recursively calls itself until it is at the end of the list, then it displays a line. In each recursive call, you should be outputting a line, otherwise it will traverse to the end and display the last line. If recursion isn't necessary, use a loop instead.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Incorrect output file: c++ (i/o) question

 
0
  #3
Nov 24th, 2008
figure out the problem! this is a recursive function...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Incorrect output file: c++ (i/o) question

 
0
  #4
Nov 24th, 2008
yeah it creates a output file each time the function is called. the way i have it setup i need a recursive function.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Incorrect output file: c++ (i/o) question

 
0
  #5
Nov 24th, 2008
Originally Posted by drjay1627 View Post
figure out the problem! this is a recursive function...
Ummm...ok? I'm pretty sure I told you the solution. Being polite will get you farther in life.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: Incorrect output file: c++ (i/o) question

 
0
  #6
Nov 24th, 2008
I replied the before I read you solution. Didn't expect a quick response. Sorry mate!

I still have the same problem though! Need to figure out a way to edit an existing file rather than create a new file. Or maybe I should create a file before the function is called...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Incorrect output file: c++ (i/o) question

 
0
  #7
Nov 24th, 2008
Open the file in append mode.

  1. ostream fOut;
  2. fOut.open("file.dat", ios::append);

I'm pretty sure that's how it's done.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC