0
void initializeVectorFromFile(vector<SInventory> & inven){

        ifstream initData("inventoryData.txt");

    if(!initData){

        cout << "File could not be accessed! Press any key to terminate program...";
        _getch();
        exit(1);

    }

    while(!initData.eof()){

        SInventory item;

        initData >> item.itemID;

        getline(initData,item.itemName);

        initData >> item.pOrdered
                 >> item.menufPrice
                 >> item.sellingPrice;
        item.pInStore = item.pOrdered;
        item.pSold = 0;

        inven.push_back(item);

        cout << item.itemID << endl;

    }

    cout << "File Read Success!" << endl;

    initData.close();
}

The .txt file I am reading from contain data structured in this order:

int
string
int double double

The output which is in the last line of the while-loop is repeated as the first itemID within the file. The initData stream does not read subsequent entries in the .txt file.

1111
1111
1111
1111
1111
...
4
  • 2
    For a start, don't use while (!whatever_file.eof()). Commented Dec 2, 2012 at 21:55
  • The homework tag is deprecated and shouldn't be used in new questions. Commented Dec 2, 2012 at 22:04
  • @JerryCoffin This was an acceptable use in the following case, why can't I utilize this method for my example and what should I implement instead. Thanks stackoverflow.com/questions/9979894/… Commented Dec 2, 2012 at 22:06
  • @Michael: No, it wasn't really acceptable there either -- you may not have noticed the problem, but if you check carefully, you'll probably find your code said it read one more record than the file actually contained. As to what to do: see my answer for one possibility. Commented Dec 2, 2012 at 22:08

2 Answers 2

3

Don't use while (!initData.eof()) -- ever. It's pretty much a guaranteed bug.

I'd start with code to read a single SInventor item from the file:

std::istream &operator>>(std::istream &initData, SInventor &item) { 
    initData >> item.itemID;

    getline(initData,item.itemName);

    initData >> item.pOrdered
             >> item.menufPrice
             >> item.sellingPrice;
    item.pInStore = item.pOrdered;
    item.pSold = 0;    
    return initData;
}

With that in place, it's probably easiest to just do without the rest of the function, and just initialize the vector directly:

std::ifstream infile("yourfile.txt");

std::vector<SInventor> inven((std::istream_iterator<SInventor>(infile)),
                              std::istream_iterator<SInventor>());

No loop, no mucked up test for EOF, etc., just a vector initialized from a pair of iterators.

Sign up to request clarification or add additional context in comments.

Comments

1

You can change your while loop to

SInventory item;
while(initData >> item.itemID){
    ...

or skip whitespace at the end of your while loop

    ws(initData);

or define an operator>>(istream&, SInventory &) and just do

SInventory item;
while(initData >> item){
    inven.push_back(item);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.