Solved

buffer or what ??

Posted on 2006-06-08
11
277 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
ID: 16866437
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
ID: 16867288
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
ID: 16868500
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
ID: 16870202
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
ID: 16870711
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:justinY
ID: 16870781
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
ID: 16870889
 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
ID: 16871176
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
ID: 16871275
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
ID: 16871537
You are right.
Thanks for your help. You are the best
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16871598
Best wishes
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 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

19 Experts available now in Live!

Get 1:1 Help Now