开发者

Initializing a Vector of Objects from a .txt file

开发者 https://www.devze.com 2023-03-17 13:11 出处:网络
#include<iostream> #include<vector> #include<fstream> #include \"stock.h\" int main(){ double balance =0, tempPrice=0;
#include<iostream>
#include<vector>
#include<fstream>
#include "stock.h"
int main(){
    double balance =0, tempPrice=0;
    string tempStr;

    vector < Stock > portfolio;
    typedef vector<Stock>::iterator StockIt;

    ifstream fileIn( "Results.txt" );
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++)
    {

        while ( !fileIn.eof( ))
        {
            getline(fileIn,tempStr);
            i->setSymbol(tempStr);

            fileIn >> tempPrice;
            i->setPrice(tempPrice);

            getline(fileIn,tempStr);
       开发者_开发技巧     i->setDate(tempStr);
        }
        fileIn.close();
    }
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++){
        cout<<i->getSymbol() <<endl;
        cout<<i->getPrice() <<endl;
        cout<<i->getDate() <<endl;
    }
    return 0;

}

Sample text file, Results.txt:

GOOG    569.964 11/17/2010
MSFT    29.62   11/17/2010
YHOO    15.38   11/17/2010
AAPL    199.92  11/17/2010

Now obviously, I want this program to create a vector of Stock Objects which has the appropriate set/get functionality for object: Stock(string, double, string).

Once that is done, I want to print out each individual member of each Object in the vector.

One thing that boggles my mind about fstream, is how can it decipher spaces and end of lines, and intelligently read strings/ints/doubles and place them into the appropriate data type? Maybe it can't...and I have to add an entirely new functionality?

now it would seem that I'm not actually creating a new object for each iteration of the loop? I think would need to do something along the lines of:

portfolio.push_back(new Stock(string, double, string));? I'm just not entirely sure how to get to that point.

Also, this code should be interchangeable with std::list as well as std::vector. This program compiles without error, however, there is zero output.


First of all, iterating over the vector only makes sense when it isn't empty. So remove the line:

for(StockIt i = portfolio.begin(); i != portfolio.end(); i++)

because otherwise the contents of this loop will never be executed.

Second, you have problems with your input reading: you use getline for the first field, which would read the values of all 3 fields on the line into the tempStr variable.

Third, you shouldn't use while(!fileIn.eof()) - the eof function only returns true after you tried to read past the end of the file. Instead, use:

while (fileIn >> symbol >> price >> date) {
    //here you should create a Stock object and call push_back on the vector.
}

This will read the three fields, which are separated by spaces.


Few issues in your code. the first for loop runs on an empty portfolio vector, as the vector is not initialized (no objects are being pushed to it) the begin() and end() are the same. you should read line by line from the fstream until EOF, then push objects to the vector. each line you read, you should split (tokenize) into the 3 parts, and create a new Stock object to be pushed to the vector.

Another side feedback, whenever using an stl iterator, use ++itr on for loops, it will run much more fast

0

精彩评论

暂无评论...
验证码 换一张
取 消