Question

I'm trying to send file data to an istream variable that stops reading before the last line and leaves before that is stored. Is there a simple way of implementing it? A stop character or something of the like?

    istream& TransactionList::getDataFromStream( istream& is) {
    //read in (unformatted) transaction list from input stream
    Transaction aTransaction;
    is >> aTransaction; //read first transaction
    while ( is != 0)    
    {       ^^^^^^^
        listOfTransactions_.addAtEnd( aTransaction);   //add transaction to list of transactions
        is >> aTransaction; //read in next transaction
    }
    return is;
}
Was it helpful?

Solution

Simple. Check that its not last. And you probably want to check for EOF.

   istream& TransactionList::getDataFromStream( istream& is) {
    //read in (unformatted) transaction list from input stream
    Transaction aTransaction;
    Transaction aNextTransaction;
    is >> aTransaction; //read first transaction
    while ( !is.eof())    
    {       ^^^^^^^
        aTransaction = aNextTransaction;
        is >> aNextTransaction; //read in next transaction
        // last read didn't set EOF, so its not the last line. Add the previous.
        if (!is.eof())
            listOfTransactions_.addAtEnd( aTransaction);   //add transaction to list of transactions
    }
    return is;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top