Advertisement
Kostiggig

Untitled

Apr 14th, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. unordered_map<int, string> topic_index_to_title_map_new;
  2.  
  3. void change_mark(string login, int index_topic, int mark) {
  4.     ifstream file;
  5.     file.open("students.txt");
  6.  
  7.     int all_lines = 0;
  8.     if (file.is_open()) {
  9.         ofstream temp_file;
  10.         temp_file.open("students_temp.txt", std::ios::app);
  11.         string questions_to_replace = "";
  12.         if (temp_file.is_open()) {
  13.             string line;
  14.             int login_divider = 13;
  15.             while (getline(file, line)) {
  16.                 if (all_lines % login_divider == 0) {
  17.                     string subbed_login = line.substr(2, line.length());
  18.                     cout << subbed_login << endl;
  19.                     if (login == subbed_login) {
  20.                         cout << " marks for " << login << endl;
  21.                         temp_file << line << '\n';
  22.                         getline(file, line);
  23.                         temp_file << line << '\n';
  24.                         getline(file, line);
  25.                         temp_file << line << '\n';
  26.                         all_lines += 2;
  27.                         int curr_topic_index = 0;
  28.  
  29.                         for (int i = 0; i < 8; i++) {
  30.                             getline(file, line);
  31.                             cout << "mark by " << topic_index_to_title_map_new[i] << " is " << line << endl;
  32.                            
  33.                             if (i == index_topic) {
  34.                                 temp_file << to_string(mark) << '\n';
  35.                                 all_lines++;;
  36.                                 cout << "Change mark for " << topic_index_to_title_map_new[i] << " to " << mark << endl;
  37.                             }
  38.                             else {
  39.                                 temp_file << line << '\n';
  40.                                 all_lines++;;
  41.  
  42.                             }
  43.                         }
  44.                     }
  45.                     else {
  46.                         temp_file << line << '\n';
  47.                         all_lines++;
  48.                     }
  49.                 } else {
  50.                     temp_file << line << '\n';
  51.                     all_lines++;
  52.                 }
  53.             }
  54.             temp_file.close();
  55.         }
  56.     }
  57.  
  58.     file.close();
  59.     ofstream file2("students.txt", ofstream::trunc);
  60.     file2.close();
  61.  
  62.     ifstream file_orig_r("students.txt");
  63.     string test_str;
  64.     while (getline(file_orig_r, test_str)) {
  65.         cout << " read from orig file after truncating " << test_str << endl;
  66.     }
  67.  
  68.     ofstream origfile("students.txt", std::ios::app);
  69.  
  70.     ifstream temp_file_r("students_temp.txt");
  71.     string temp_line;
  72.  
  73.     int curr_line = 0;
  74.     while (getline(temp_file_r, temp_line)) {
  75.         if (curr_line == all_lines) {
  76.             cout << "-2 line is " << temp_line;
  77.             origfile << temp_line;
  78.         }
  79.         else {
  80.             origfile << temp_line << '\n';
  81.         }
  82.         curr_line++;
  83.     }
  84.  
  85.     ofstream temp_file_n("students_temp.txt", ofstream::trunc);
  86.     temp_file_n.close();
  87.  
  88.     temp_file_r.close();
  89.     origfile.close();
  90. }
  91.  
  92. topic_index_to_title_map_new[0] = "Loop";
  93.     topic_index_to_title_map_new[1] = "Array";
  94.     topic_index_to_title_map_new[2] = "String";
  95.     topic_index_to_title_map_new[3] = "Recursion";
  96.     topic_index_to_title_map_new[4] = "Struct";
  97.     topic_index_to_title_map_new[5] = "File";
  98.     topic_index_to_title_map_new[6] = "Pointers";
  99.     topic_index_to_title_map_new[7] = "Dynamic";
  100.  
  101.     string login = "loginK9877";
  102.     change_mark(login, 4, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement