Advertisement
sNow_32

Read from CSV

Nov 3rd, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. void open_from_file()
  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.                 std::getline(file, Hotel[n].id, ';');
  29.                 std::getline(file, Hotel[n].date_1, ';');
  30.                 std::getline(file, Hotel[n].date_2, ';');
  31.                 std::getline(file, Hotel[n].name, ';');
  32.                 std::getline(file, Hotel[n].second_name, ';');
  33.  
  34.                 /*ostringstream temp;
  35.                     temp << Hotel[n].money;
  36.                     string cost = temp.str();               (int to str)
  37.                     std::getline(file, cost, ',');*/
  38.  
  39.                 std::getline(file, Hotel[n].money, ';');
  40.                 std::getline(file, Hotel[n].number_phone, ';');
  41.                 n++;
  42.             }
  43.         cout << "\n\t(!) Reading successfully! Objects: " << n <<".\n\t\tOpened: save_file.csv" << endl;
  44.         }
  45.     }
  46.     file.close();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement