Read from a file. C++ -


so when program starts, attempts read list of products file. if file not exist displays error , continue on. problem im having when displays error, doesnt continue on while loop

ifstream input;     input.open("data.txt");       if (input.fail())     {         cout << "\n data file not found \n";     }     listitemtype data;       input >> data.productname;     while(( !input.eof()))     {         input >> data.category;         input >> data.productprice;         addproduct(head, data);          input >> data.productname;     }      input.close(); 

it's not identical functionality, it's better move towards like:

if (std::ifstream input("data.txt")) {     listitemtype data;      while (input >> data.productname >> data.category >> data.productprice >> data.productname)         addproduct(head, data);     if (!input.eof())         std::cerr << "error parsing input file.\n"; }     else     cout << "\n data file not found \n"; 

if structure if/else clauses above, whatever happens continue following code you'd like.

note code above checks problem after each input operation. code tries read data.productprice if reading data.category failed. it's kind of weird you're reading productname twice, , i'm assuming can call addproduct after i/o - if not you'll need while loop like:

    while (input >> data.productname >> data.category >> data.productprice)     {         addproduct(head, data);         if (!(input >> data.productname))             break;     } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -