Solved

File output strange problem

Posted on 2004-04-26
9
325 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

821 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