| | |
Incorrect output file: c++ (i/o) question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 80
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 application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





