Solved

File output strange problem

Posted on 2004-04-26
9
328 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:lordiano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10919504
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.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10919507
BTW: Are you doing any file operations in the "boring part"?
0
 

Author Comment

by:lordiano
ID: 10919551
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
ID: 10919836
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.
0
 

Author Comment

by:lordiano
ID: 10919929
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?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10920046
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
0
 

Author Comment

by:lordiano
ID: 10920067
err, ops i am sorry itsmeandnobodyelse...
you came in 5 seconds late......
thanks for your help :)
0
 

Author Comment

by:lordiano
ID: 10920094
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 :(  )
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10920132
>> 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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question