-3

I want my program to be able to remember where it left off in a .txt file in order to proceed to the next input upon reiteration through a loop. For instance, a text file containing:

Apples
Bananas
Oranges

would be accessed through a function GetItem() that appends the next file input into a vector of items. How do I make the function add Apples the first time, Bananas the second time, and Oranges the third iteration? As of now, each call to GetItem() keeps adding the first element to the vector, giving a vector containing:

Apples
Apples
Apples

Because the file keeps opening from the beginning. Any help would be appreciated.

This is a simplified version of lengthy amounts of code that I could include, but would distract from the main purpose of the question. If the code is needed, I would be happy to include it.

vector<Item*> AddItemToInventory(vector<Item*> inventory) {
if (inptLctn == 'f') {
        inptFile.open("TestFood.txt");
        if (!inptFile.is_open()) {
            cout << "Could not open file." << endl;
            exit(1);
            }
            inptFile >> usrInptName;
            inptFile >> usrInptQnty;
            inptFile >> usrInptExpr;
            inptFile >> usrInptPrice;

            }
prdc = new Produce;
    prdc->SetName(usrInptName);
    prdc->SetQuantity(usrInptQnty);
    prdc->SetExpiration(usrInptExpr);
    prdc->SetPrice(usrInptPrice);
    inventory.push_back(prdc);

    return inventory;
}
ntjess
  • 39
  • 2
  • 4
    Why are you closing the file? – Mooing Duck Aug 03 '16 at 22:37
  • 4
    How about: don't close and open the file every time? – user253751 Aug 03 '16 at 22:38
  • @Mooing Duck Yes, but when I took out the statement, the same problem occurred. – ntjess Aug 03 '16 at 22:40
  • @immibis Is there a way to open the file only once, iterate through as many times as needed, and close at the end? Because the file is used in a function call outside main, so each end to the function call restarts the process. – ntjess Aug 03 '16 at 22:41
  • 4
    Please share you code to allow us to make an educated guess. :D and yes or you are closing and opening the file in each iteration or your pointer to the file is not moving... – DIEGO CARRASCAL Aug 03 '16 at 22:42
  • 2
    @ntjess That's the normal way to process a file, and it happens automatically if you don't close and re-open it. – Barmar Aug 03 '16 at 22:42
  • use [fopen()](http://www.cplusplus.com/forum/beginner/22558/) then at the end of the looping fclose... see the __[example](http://www.cplusplus.com/forum/beginner/22558/#msg118123)__ – DIEGO CARRASCAL Aug 03 '16 at 22:43
  • You return a vector with only one item. Why not read all the items and return all of them in that vector. – Zan Lynx Aug 03 '16 at 22:51
  • 2
    Change it to `AddItemToInventory(vector inventory, ifstream &inptFile)` then have the caller in charge of opening and closing the `ifstream`? – user253751 Aug 03 '16 at 22:56

1 Answers1

3

Open the file before you use it and close it after you are done using it. The problem is that you are continually closing the input file so when you re-open it, it starts at the beginning again.

You should only open and close your input file once.

Jesse
  • 73
  • 6
  • Ok, thanks. Does this mean I should open and close the file in main? – ntjess Aug 03 '16 at 22:48
  • Because I run into scope problems doing this :/ – ntjess Aug 03 '16 at 22:54
  • Open yes. It will close itself when it goes out of scope. – user4581301 Aug 03 '16 at 22:54
  • Sure, you can open in main. Then you have to pass the input stream by reference to the calling function. If you pass by value, the copy of the input stream object in the calling function will not have an association with the file. – Jesse Aug 04 '16 at 01:17