storing contents of file into variables in c++ -


i'm working on program requires me create hash table contents of file. file contains records (1 per line) include key (int), name (string), code (int), , cost (double). have code written of program create hash table, however, i'm having trouble figuring out how should load table file. more specifically, how store each piece of record corresponding variable?

the code kind of long, if think posting answer question, let me know , i'll glad include it.

i feel should include though, have struct set hold information contained in each record have set follows:

struct record {         int key;         string name;         int code;         double cost; } 

if need see other portions of code, or code whole, let me know.

if data file has white-space separated items in lines, can use example following code:

#include <iostream> #include <fstream>  using namespace std;  typedef struct record {     int key;     string name;     int code;     double cost; } rec;  int main() {     ifstream f ("data.txt");      rec r;      f >> r.key;     while (!f.eof())     {             f >> r.name;             f >> r.code;             f >> r.cost;              cout << "new record, key=" << r.key                     << ", name=" << r.name                     << ", code=" << r.code                     << ", cost=" << r.cost << "\n";             f >> r.key;     }      //will done destructor: f.close();      return 0; } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -