File output strange problem

I have written a class that does basic compression and decompression on audio .raw files..
It works fine when its a stand-alone class. Recently I am trying to merge the class to my managed Form.
It does not give the correct outfile anymore..
anyway this is the code that does encoding:
void smartACD::encode(string filename, string outfilename, int k)
  unsigned char first, second;
  short int c, c_new, qs, d;
  unsigned short int towrite;
  int oremain, iremain;
  unsigned char toput = 0x00;
  ifstream infile(filename.c_str());
  ofstream outfile(outfilename.c_str());
... a bit too long so i will skip the boring part

    first = infile.get();
    second = infile.get();
    c_new = second;
    c_new <<= 8;
    c_new |= first;

    d = c_new - c;
    c_new = 0;
    if(d >= qs)
      d = qs - 1;
    else if(d <= (-qs))
      d = 1 - qs;
    c = c + d;
    towrite = d + qs - 1;
    ihave = k;
    while(ihave > 0){
      tmp = towrite << (16-ihave);
      tmp >>= (16-ihave);
      if(ihave > ineed){
      toput |= tmp >> (ihave - ineed);
      i = 0;
      toput = 0;
      ihave -= ineed;
      ineed = 8;
      toput |= tmp << (ineed - ihave);
      ineed -= ihave;
      ihave = 0;
      i = 1;
This would work perfect fine if i just make a driver for it and call it in unix shell..
Here is how I create it in my windows Form:

    public __gc class Form1 : public System::Windows::Forms::Form
        smartACD * myACD;
            myACD = new smartACD();
...Skipping useless part

//Here is where I call it:
private: System::Void menuItem2_Click(System::Object *  sender, System::EventArgs *  e)
                   myACD->encode("music.raw", "music_en.raw", 3);

I use breakpoint and does step into, step over, and found out that it runs the encode function, but the output file is just not of the correct size for some reason. In fact, it always generated some 1kb file no matter how big the raw file i put in.
I am guessing there is something incompatible with the vc++ fstream , OR, there is some environment setting which i didn't turn on .

Please give me a hand on this one ! thanks
500 points for the length of this post
Who is Participating?
Karl Heinz KremerConnect With a Mentor Commented:
I am pretty sure that the problem is not in the handling of your output file but in the code that actually processes information, and it probably has to do with differences between your Unix system and the Windows/PC architecture (e.g. different sizes of data structures or something similar). The only way to find these types of problems is with running in the debugger and doing all the calculations the software is supposed to do in parallel on paper.
Karl Heinz KremerCommented:
When you set a breakpoint on the two lines with outfile.put(toput), is the correct output actually written to the file? If that's not the case, you need to find out why the toput variable does not contain the correct values, or why outfile.put() is not called correctly.

I would start with setting breakpoints on the two lines I've mentioned and then run until the first output operation is performed. At that point, you need to find out if the input values you've read so far can create the output value you are looking at. If htat's not correct, then backtrack to the last point where the values are correct.

Without knowing how your mechanism should work, your code looks correct.
Karl Heinz KremerCommented:
BTW: Are you doing any file operations in the "boring part"?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

lordianoAuthor Commented:
Hi khkremer thanks for input, basically i am only using : outfile.put(ch)  to write to file, in the boring part i think there is a couple outfile.put(ch) as well..
nothing else..
I just found out that my jpeg codec is having the same problem as well.. I could run them perfectly fine under unix but when i put the classes in VC++ it ouput the same 1 KB junk file instead of the encoded files.
I am gonna go ahead and try out the checking value thing right now, will get back to you in a few.
lordianoAuthor Commented:
it seems that infile.get() is not getting a single unsigned char???
I am not so sure why but this syntax is valid in c++..
I checked the number of times it loops in the while(infile.good()) and found that when i run it under unix,
it returns 70818 times, and running the same file under windows is only 278 times.
THats strange.....
I am not so familiar with VC++ syntax.. do you know if i can even use get() at all to read in 1 single byte?
You may try to explicitly set input and outfile to binary as Windows makes some funny thing when reading a binary file in text mode, so converting any CRLF pair to a single LF (linefeed).  

ifstream infile(filename.c_str(), ios_base::in | ios_base::binary);
ofstream outfile(outfilename.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);

Hope, that helps

lordianoAuthor Commented:
err, ops i am sorry itsmeandnobodyelse...
you came in 5 seconds late......
thanks for your help :)
lordianoAuthor Commented:
yes, i finally figured out that i did not open the file as binary (since in unix it doesn't matter).
thanks for all the help, still feeling sorry for Alex, you gave correct answer (5 seconds after i awarded points :(  )
>> it seems that infile.get() is not getting a single unsigned char???

That's because default ifstream has a short as character type (TCHAR) because UNICODE macro is set on your system.

You should use

     infile >> first;
     infile >> second;

to read characters rather than TCHAR

Regards, Alex

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.