Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:56:57+00:00 2026-06-15T06:56:57+00:00

void initializeVectorFromFile(vector<SInventory> & inven){ ifstream initData(inventoryData.txt); if(!initData){ cout << File could not be accessed!

  • 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
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:56:59+00:00Added an answer on June 15, 2026 at 6:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
void expand_combinations(const char *remaining_string, string const & s, int rema in_depth) { if(remain_depth==0) {
void Insert(AVLnode * & root, string X) { if ( root == NULL) {
void* myfunction() { char *p; *p = 0; return (void*) &p; } I know
void openUpNow(FILE *x, FILE *y) { x = fopen(xwhatever, r); y = fopen(ywhatever, r);
void slice_first_char(char ** string) { *string = &(*string[1]); } int main(int argc, char *
void display(char * str){ printf(%s: Missing file\n, str); } int main(int argc, char **argv)
void getS(char *fileName){ FILE *src; if((src = fopen(fileName, r)) == NULL){ printf(%s %s %s,
void GameBoard::print(const GameBoard& computerBoard) { Grid[0][0] = '1'; Grid[0][1] = '2'; Grid[1][0] = '3';
void display() { list *newlist; newlist = first; cout << endl; do { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.