Solved

buffer or what ??

Posted on 2006-06-08
11
266 Views
Last Modified: 2010-04-01
Hi experts,

I have a file

Sale Date: ,,,05052006
transaction, ,Sale Agent, Notes, Sell to, Sell Amt
sell, ,12345,store123,100.00


Here is what I do.

Step 1 - Take the sale date, I have no problem

if (row_elem[0].find("Sale Date") != string::npos)
{
            DateConvert(row_elem[3]);

            cout << row_elem[3] << endl; // here shows 05052006
                                                                        
}

step 2 - I need write to a out put file like this

if (row_elem[0] == "Sell")
{
      fout<<setw(2)            << setiosflags(ios::left) << "AM"
            <<setw(20)      << setiosflags(ios::left) << "12345 "
            <<setw(16)      << setiosflags(ios::left) << " "
            <<setw(8)       << setiosflags(ios::left) << row_elem[3} //sale date here
}

the right output file should be -
AM12345              05052006

But my output file is wrong like this
AM12345              store123//here does not show 05052006

How can I fix this, please help
Do I need buffer to memorize the date and write to the output file ? and How ?

Thanks
0
Comment
Question by:justinY
11 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
Hi justinY,

Could you show us the code for DateConvert? I suspect it is freeing memory it shouldnt be.


Paul
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Please do mention the datatype of  row_elem.

>> cout << row_elem[3] << endl; // here shows 05052006
Is this statement also printing the same value ?
Or, the correct value ?
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Presumably row_elem[3] is reinitialised with each row that you read from the file. Why not declare a variable outside your processing loop and call it sale_date? Initialise sale_date, when row_elem[0].find("Sale Date") != string::npos and output sale_date (along with the sell data), when row_elem[0] == "Sell".
0
 

Author Comment

by:justinY
Comment Utility
Thanks guys,
1.  DateConvert code.

void DateConvert(string &date)
{
    int day, month, year;
    if ( sscanf ( date.c_str(), "%d/%d/%d", &month, &day, &year ) == 3 )
    {
        char newdate[32];
        sprintf(newdate, "%02d%02d%04d", month, day, year );
        date = newdate;
    }
};

2. Data type of Row_elem
 
vector<string> row_elem;

3. Basically, I want to grab the date 05052006 and write it to output file like this
if (row_elem[0] == "Sell")
{
     fout<<setw(2)          << setiosflags(ios::left) << "AM"
          <<setw(20)     << setiosflags(ios::left) << "12345 "
          <<setw(16)     << setiosflags(ios::left) << " "
          <<setw(8)      << setiosflags(ios::left) << row_elem[3]
}
so the right output file should be -
AM12345              05052006

but my problem is when I write to output file I have
AM12345              store123

Please help, thanks
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
row_elem[3] is overwritten, when...

   sell, ,12345,store123,100.00

...is parsed.

You need to preserve a copy of the row_elem[3] read from...
   
   Sale Date: ,,,05052006

...and use that instead of the 3rd column from...
 
      sell, ,12345,store123,100.00

...which is indeed store123.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:justinY
Comment Utility
I agree with you. But how can I preserve a copy of the row_elem[3] ? I try to use memcpy, but I have garbage on my screen.
Can you tell me how ?

Thanks
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
Comment Utility
 string sale_date = "?";

  while ( /* Read line from file as before */ ) {

        /* Load row_elem vector as before */

        if (row_elem[0].find("Sale Date") != string::npos)
        {
                  sale_date = row_elem[3];   // Did you want this before or after conversion?
                  DateConvert(row_elem[3]);
       
                  cout << row_elem[3] << endl; // here shows 05052006
                                                                   
        }


        if (row_elem[0] == "Sell")
        {
             fout<<setw(2)          << setiosflags(ios::left) << "AM"
                  <<setw(20)     << setiosflags(ios::left) << "12345 "
                  <<setw(16)     << setiosflags(ios::left) << " "
                  <<setw(8)      << setiosflags(ios::left) << sale_date << '\n';
        }

  }
0
 

Author Comment

by:justinY
Comment Utility
We are almost there, except one thing.

I have multiple lines of records. But my first line shows ? on sale_date. other lines are ok.
like this
AM12345              ?
AM12346              05052006
AM12347              05052006
AM12348              05052006

I think this is because we initilize sale_date ="?" outside of while loop. How can we fix that ? Thanks
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
That means that your first Sell record is not preceded by a Sale Date record. You don't know what date it is.

You could apply some business logic that you collect the first Sell record(s) with unknown Sale Date (store them in a list) and apply next Sale Date, when it is known, but that makes the assumption that those records were indeed for the same Date. Alternatively, you could apply the business logic that you junk them while the Sale Date is unknown.
0
 

Author Comment

by:justinY
Comment Utility
You are right.
Thanks for your help. You are the best
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Best wishes
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

  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 …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

772 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

8 Experts available now in Live!

Get 1:1 Help Now