473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.

Expand|Select|Wrap|Line Numbers
  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!
Nov 24 '08 #1
1 1548
vmpstr
63
I can explain the problem to you, but the fix is up to you (because my C++ file i/o abilities aren't particularly strong).

The problem you have is that in a recursive function, you keep opening the same file for writing. The first time you write, and return, the file stores the first line of output.

The second time you write, the file is opened again (thus overwriting the old file) and a second line is written.

So on, and so forth, until the last line. The last line overwrites the second last line, and the program terminates. That is why you have only the last line in the file.

The solution is to either open the file once outside of the function call, or to reopen it with an append mode (this is where I can't help with code, because I'm not quite sure how to do this) before writing and to make sure to close it after writing.

There might be some other effect you have with opening files that you do not close until the function exists, but it's "probably ok" (meaning, you shouldn't have it). That is, the first call to the function does not write anything, but it keeps an open handle until every other call of the function has written, and only closes the file afterwards... This might cause some weird behaviour, but then again it might not.
Nov 24 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
6
by: Dario Di Bella | last post by:
Hi all, I hope someone can help me on this. I need to parse the following XML: .... <area name="promotore"> <item id="004" code="003" description="attivita promotore"> <!]> </item> </area>...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
4
by: teddysnips | last post by:
I am trying to insert a row into a table using a stored procedure and I get the following error if I try this from QA: INSERT failed because the following SET options have incorrect settings:...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
3
by: Ron Jackson | last post by:
I am using Python 2.5 on Windows XP. I have installed Pyserial and win32all extensions. When I try to run the example program scan.py (included below), or any other program using pyserial, as...
1
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please...
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.