Solved

File output strange problem

Posted on 2004-04-26
9
316 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now