Reputation: 1
I am making a huffman tree and I have completed my work. But a problem has occured when I read a char, it reads zero instead of space " " in the first part .
Here is the code. The error occurs in the decode
function during file handling.
void huffmantree<h>::decode(){
CString *st;
hscll<h> * temp=new hscll<h> ();
int h;
ifstream myfile;
myfile.open ("z://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaae.txt");
myfile >>h;
cout<<h;
for(int i=0;i<h;i++)
{char temp9[100];
char l;
myfile >>l;
temp->insert(l);
// myfile >> l ;
myfile >> temp9;
temp->atindex(i)->symboc.symbol=l;
temp->atindex(i)->code->setString(temp9);
//myfile >> '\n';
}
_getch();
//CString u;
//char k;
//int p=0;
//for(int i=0;i<st->m_length;i++)
//{
// for(int j=0;j<8;j++)
// {
//
// char a;
// a=st->charAt(i);
// int temp=1;
// if(j==0)
// temp=1;
// if(a!='0')
// {
// for(int y=0;y<j;y++)
// {
// temp*=2;
// }
// p+=temp;
// }
// else
// {
// p+=0;
//
// }
// i++;
// cout<<a;
// }
// k=p;
// char t=7;
// //cout<<" "<<t<<" "<<p<<endl;
// myfile << k;
// p=0;
// u.addcharbychar(k);
//}
////cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<u.getString();
////cout<<endl<<endl<<endl<<"the length is =="<<u.m_length<<endl;
myfile.close();
}
Upvotes: 0
Views: 111
Reputation: 153899
If you use >>
for input, leading whitespace will be skipped. If you're reading binary data, you need to open the file in binary mode (myfile.open( name, std::ios::in | std::ios::binary
) and use the non-formatting input functions, like istream::get()
. (And when writing, you'll also need to write binary.)
Upvotes: 2