Advertisement
Kostiggig

Untitled

Apr 14th, 2023
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. using namespace std;
  2.  
  3. vector<string> split_string_lol(const string& s, char delimiter) {
  4.     vector<string> tokens;
  5.     size_t start = 0, end = 0;
  6.     while ((end = s.find(delimiter, start)) != string::npos) {
  7.         tokens.push_back(s.substr(start, end - start));
  8.         start = end + 1;
  9.     }
  10.     tokens.push_back(s.substr(start));
  11.     return tokens;
  12. }
  13.  
  14. void add(string questions_arr[], int size, string login) {
  15.     ifstream file;
  16.     file.open("estimation/progress.txt");
  17.  
  18.     int all_lines = 0;
  19.     if (file.is_open()) {
  20.         ofstream temp_file;
  21.         temp_file.open("estimation/progress_temp.txt", std::ios::app);
  22.         string questions_to_replace = "";
  23.         if (temp_file.is_open()) {
  24.             string line;
  25.             while (getline(file, line)) {
  26.                 all_lines++;
  27.                 if (login == line) {
  28.                     temp_file << line << '\n';
  29.                     string questions;
  30.                     getline(file, questions);
  31.                     cout << "q to replace " << questions << endl;
  32.                     questions_to_replace = questions;
  33.                     string new_questions = "";
  34.                     for (int i = 0; i < size; i++) {
  35.                         questions = questions + "@" + questions_arr[i];
  36.                     }
  37.                     temp_file << questions << '\n';
  38.                 }
  39.                 else {
  40.                     temp_file << line << '\n';
  41.                 }
  42.             }
  43.  
  44.            
  45.             file.close();
  46.  
  47.             ofstream file2("estimation/progress.txt", ofstream::trunc);
  48.             file2.close();
  49.  
  50.             ofstream origfile("estimation/progress.txt", std::ios::app);
  51.  
  52.             temp_file.close();
  53.             ifstream temp_file_r("estimation/progress_temp.txt");
  54.             string temp_line;
  55.  
  56.             int curr_line = 0;
  57.             while (getline(temp_file_r, temp_line)) {
  58.                 if (curr_line == all_lines) {
  59.                     cout << "-2 line is " << temp_line;
  60.                     origfile << temp_line;
  61.                 }
  62.                 else {
  63.                     origfile << temp_line << '\n';
  64.                 }
  65.                 curr_line++;
  66.             }
  67.            
  68.             temp_file.close();
  69.             temp_file.open("estimation/progress_temp.txt", ofstream::trunc);
  70.             temp_file_r.close();
  71.             origfile.close();
  72.         }
  73.        
  74.     }
  75.     else {
  76.         std::cout << "Unable to open file.\n";
  77.     }
  78.  
  79.    
  80.    
  81. }
  82.  
  83. string login = "loginnewone356";
  84.  
  85.     ifstream file("estimation/progress.txt");
  86.     string str;
  87.     int curr = 0;
  88.     bool user_was_not_found = true;
  89.     while (getline(file, str)) {
  90.         if (login == str) {
  91.             user_was_not_found = false;
  92.             string ques;
  93.             getline(file, ques);
  94.  
  95.             if (ques == "") {
  96.                 cout << "Questions for " << login << " are empty" << endl;
  97.             }
  98.             else {
  99.                 cout << "Questions for " << login << endl;
  100.                 vector<string> qs = split_string_lol(ques, '@');
  101.                
  102.                 for (const string& q : qs) {
  103.                     if (q != "") {
  104.                         cout << "q is " << q << endl;
  105.                     }
  106.                 }
  107.                
  108.             }
  109.         }
  110.         curr += 2;
  111.     }
  112.  
  113.     if (user_was_not_found) {
  114.         std::ofstream outfile;
  115.         outfile.open("estimation/progress.txt", std::ios::app); // open file in append mode
  116.  
  117.         if (outfile.is_open()) {
  118.             outfile << "\n";
  119.             outfile << login << "\n";
  120.             outfile.close();
  121.         }
  122.         else {
  123.             std::cout << "Unable to open file.\n";
  124.         }
  125.     }
  126.  
  127.     string questions[3];
  128.     questions[0] = "addresses:5";
  129.     questions[1] = "addresses:7";
  130.     questions[2] = "addresses:35";
  131.     add(questions, 3, login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement