Link to home
Start Free TrialLog in
Avatar of lordiano
lordiano

asked on

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 VC++.net 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
...

while(infile.good()){
    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);
      outfile.put(toput);
      i = 0;
      toput = 0;
      ihave -= ineed;
      ineed = 8;
      }
      else{
      toput |= tmp << (ineed - ihave);
      ineed -= ihave;
      ihave = 0;
      i = 1;
      }
    }
  }
  if(i)
    outfile.put(toput);
  outfile.close();
  infile.close();  
}
=========================================================================
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
    {  
    public:
        smartACD * myACD;
        Form1(void)
        {
            InitializeComponent();
            myACD = new smartACD();
        }
...
...Skipping useless part
...

//Here is where I call it:
private: System::Void menuItem2_Click(System::Object *  sender, System::EventArgs *  e)
             {
                   StartThread2();
                   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
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

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.
BTW: Are you doing any file operations in the "boring part"?
Avatar of lordiano
lordiano

ASKER

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.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Alex
err, ops i am sorry itsmeandnobodyelse...
you came in 5 seconds late......
thanks for your help :)
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