943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 722
  • C++ RSS
Nov 24th, 2008
0

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

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Nov 24th, 2008
0

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

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.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is online now Online
774 posts
since Nov 2007
Nov 24th, 2008
0

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

figure out the problem! this is a recursive function...
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Nov 24th, 2008
0

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

yeah it creates a output file each time the function is called. the way i have it setup i need a recursive function.
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Nov 24th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by drjay1627 ...
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.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is online now Online
774 posts
since Nov 2007
Nov 24th, 2008
0

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

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...
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Nov 24th, 2008
0

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

Open the file in append mode.

C++ Syntax (Toggle Plain Text)
  1. ostream fOut;
  2. fOut.open("file.dat", ios::append);

I'm pretty sure that's how it's done.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is online now Online
774 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem using Pointer Functions
Next Thread in C++ Forum Timeline: Disable/Enable Menu Items and Sub Items





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC