| | |
Incorrect output file: c++ (i/o) question
![]() |
•
•
Join Date: Nov 2008
Posts: 75
Reputation:
Solved Threads: 1
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.
This is method. Can someone please help me!
C++ Syntax (Toggle Plain Text)
void traverse(string code = "") const { string outputFile = "huffman.txt"; ofstream outfile(outputFile.c_str()); if (child0 !=NULL) { static_cast<huffman_item<dtype> >( *child0).traverse(code + '0' ); static_cast<huffman_item<dtype> >( *child1).traverse(code + '1' ); } else{ outfile << data << freq << code << endl; cout << " Data: " << data << " Frequency: " << freq << " Code: " << code << endl; } }
This is method. Can someone please help me!
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Open the file in append mode.
I'm pretty sure that's how it's done.
C++ Syntax (Toggle Plain Text)
ostream fOut; fOut.open("file.dat", ios::append);
I'm pretty sure that's how it's done.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Please help me "I WANT IT FOR TOMORROW" (C++)
- Double linked list - possible memory error (C)
- c++ scores (C++)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- seg fault that doesnt make sense (C++)
- YaBasic: The beginners choice (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Need Some Help in programs
- Next Thread: Disable/Enable Menu Items and Sub Items
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets





