Advertisement
sNow_32

Read

Nov 18th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. void Hotel::Read(Hotel &hotel)
  2. {
  3.     ifstream file;
  4.     string buff;
  5.     file.open("save_file.csv", ios::in);
  6.     n = 0;
  7.     file >> buff;
  8.     if (!file.is_open()){
  9.         cout << "(!) File doesn't exist!\nCreate it? [Y/N]\n" << endl;
  10.         cout << "~ ";
  11.         string create_file; cin >> create_file;
  12.         if (create_file == "Y"||create_file == "y"
  13.                 ||create_file == "Р”"||create_file == "Рґ")
  14.         { ofstream file;
  15.             file.open("save_file.csv", ios::out);
  16.             cout << "(!) File created!" << endl;}
  17.         else cout << "Creating aborted..." << endl;
  18.         file.close();
  19.     }
  20.     else {
  21.         if (buff.empty())
  22.             cout << "(!) File is empty..." << endl;
  23.         else {
  24.             ifstream file;
  25.             file.open("save_file.csv", ios::in);
  26.             while (!file.eof())
  27.             {
  28.                 string s_id = std::to_string(id);                    /* all to str */
  29.                 string s_cost = std::to_string(cost);
  30.                 string s_name(name);
  31.                 string s_fname(fname);
  32.                 string s_date_1(date_1);
  33.                 string s_date_2(date_2);
  34.                 string s_phoneNumber(phoneNumber);
  35.  
  36.  
  37.                 std::getline(file, s_id, ';');
  38.                 std::getline(file, s_name, ';');
  39.                 std::getline(file, s_fname, ';');                         /*Read from csv*/
  40.                 std::getline(file, s_date_1, ';');
  41.                 std::getline(file, s_date_2, ';');
  42.                 std::getline(file, s_phoneNumber, ';');
  43.                 std::getline(file, s_cost, ';');
  44.  
  45.                 id = atoi(s_id.c_str());                                    /*str to int*/
  46.                 cost = atoi(s_cost.c_str());
  47.                 const char *r_name = s_name.c_str();           /*str to char*/
  48.                 strcpy(name, r_name);                                     /*const char to char*/
  49.                 const char *r_fname = s_fname.c_str();
  50.                 strcpy(fname, r_fname);
  51.                 const char *r_date_1 = s_date_1.c_str();
  52.                 strcpy(date_1, r_date_1);
  53.                 const char *r_date_2 = s_date_2.c_str();
  54.                 strcpy(date_2, r_date_2);
  55.                 const char *r_phoneNumber = s_phoneNumber.c_str();
  56.                 strcpy(phoneNumber, r_phoneNumber);
  57.  
  58.                 lst.Add(hotel);
  59.                 n++;
  60.             }
  61.  
  62.             cout << "\n\t(!) Reading successfully! Objects: " << n <<".\n\t\tOpened: save_file.csv" << endl;
  63.         }
  64.     }
  65.     file.close();
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement