Thread: File Output Assigning Incorrect Values From Vector (C++)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1

    Question File Output Assigning Incorrect Values From Vector (C++)

    I'm working with a C++ application that produces a file based on a database query using the RogueWave Tools. The compiler is Visual Studio C++ 6.0. Once the user chooses a "batch", the code runs the CreateFile method below, which runs a query, loading the selected rows from the database table into a vector, which is then loaded into a RogueWave type RWTPtrOrderedVector<RWCString>, which is then printed to the file.

    The problem is, field "addr2" in the produced file is sometimes duplicating a previous record's value (usually a the one from 10 records previous, but sometimes 20 records previous). The database query reveals there are no duplicate values in the addr2 field for the batch, so I'm assuming the problem must be in the C++ app. For example...

    rownum order addr addr2
    ------ ----- ---- ----
    1 1001 100 Apple St. Suite 4
    2 1002 546 Plum Ave.
    3 1003 224 Peach Ln. Suite B
    4 1004 471 Grape St.
    ...
    13 1013 334 Banana Ct. Suite B (duplicate - should be emply value here)

    This problem doesn't appear to be consistent, e.g. doesn't always happen every 10 records, and sometimes doesn't happen at all. I'm wondering if this could be a buffer issue of some sort.

    Thanks for any help and/or insight on this!


    Code:
    // MAIN ROUTINE
    CError* CreateFile(const CBatch& aBatch)
    {
        CError *error = NULL;
        int ord_cnt=0;
        RWTPtrOrderedVector<COrders> order_vector;
        COrd order_ctlr(*m_Db);
    
        ord_cnt = order_ctlr.OrdsForClient(
    	aBatch.ClientCode(),
    	aBatch.BatchNumber(),
    	order_vector,
    	*m_Connection);
    
        // LOAD EACH VECTOR RECORD INTO m_Holder, WHICH
        // IS OF TYPE RWTPtrOrderedVector<RWCString>
        for (int i=0; i<ord_cnt; i++)
        {
    	MakeFile(*order_vector[i]);
        }
    
        char filename[200];
        m_FormFilenameForShippingFile('myfile', aBatch);
        error = WriteFile(filename, m_Holder);
        m_Holder.clearAndDestroy();
    
        return error;
    }
    
    
    // RUN DB QUERY AND LOAD ROWS INTO VECTOR
    int COrd::OrdsForClient (
        int client_code, 
        const RWCString& batch_number, 
        OrderVector& vec,
        const RWDBConnection& aConn)
    {
        RWCString sBatchNumber = batch_number;
    
        RWDBSelector aSelector = m_Db.selector();
        RWDBTable ship_table = m_Db.table("ship_table");
    
        aSelector << m_Table;
        aSelector.where (
    	m_ClientCodeColumn == RWDBBoundExpr(&client_code)
    	&& m_BatchNumberColumn == RWDBBoundExpr(&sBatchNumber)
    	&& m_ShippingMethodColumn == ship_table["ship_method"]
        );
    
        aSelector.orderBy (shipping_table["priority"]);
        aSelector.orderBy (m_StatusCodeColumn);
        aSelector.orderBy (m_OrderNumberColumn);
    
        RWDBReader aReader = aSelector.reader(aConn);
    
        while (aReader())
        {
    	COrders *aOrder = new COrders();
    	aReader >> *aOrder;
    	vec.insert(aOrder);
        }
        return vec.entries();
    }
    
    
    //ASSIGN - APPEND FORMATED ROWS TO THE m_Holder vector
    // of type RWTPtrOrderedVector<RWCString>
    void MakeFile(const COrders& aOrder)
    {
        char string[500];
    
        sprintf( 
    	string, 
    	"%-11.11s%-10.10s%-44.44s%-35.35s%-35.35s%-35.35s%-30.30s%2.2s%5.5s-%-53.53s%-10.10s%-6.6s",
    	aOrder.BatchNumber(),
    	aOrder.OrderNumber(),
    	aOrder.ShiptoName(),
    	aOrder.ShiptoCompany(),
    	aOrder.ShiptoAddress1(),
    	aOrder.ShiptoAddress2(),
    	aOrder.ShiptoCity(),
    	aOrder.ShiptoState(),
    	aOrder.ShiptoZip5(),
    	"",
    	aOrder.ShiptoPhone()
        );
    
        m_Holder.append( new RWCString(string) );
    }
    
    
    // Write each line from the Vector to the file
    CError* WriteFile(const char *pFileName, const RWTPtrOrderedVector<RWCString>& stringVector)
    {
        CError *err = NULL;
        TRY
        {
    	CStdioFile f( pFileName,  CFile::modeCreate | CFile::modeWrite);
    	CString s;
    
    	for (int i = 0; i < (int)stringVector.length(); i++)
    	{
    	    s.Format("%s\n",*stringVector[i]);
    	    f.WriteString( (const char *)s );
    	}
    		
    	s = "OK!\tFile: ";
    	s += pFileName;
    
        //	AfxMessageBox(s);
        }
        CATCH( CFileException, e )
        {
    	// EXCEPTION CODE HERE
        }
        END_CATCH
    	
        return err;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not familiar with these RogueWave classes, but if they are anything like the standard library classes you're problem might be that you aren't checking the state of the reader until after you attempt to read in a value and push it back on the vector. Consider doing the following to see if it helps:
    Code:
        while (aReader())
        {
            COrders *aOrder = new COrders();
            aReader >> *aOrder;
            if (aReader())
                vec.insert(aOrder);
            else
                delete aOrder;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM