Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- vector<string> split_string_lol(const string& s, char delimiter) {
- vector<string> tokens;
- size_t start = 0, end = 0;
- while ((end = s.find(delimiter, start)) != string::npos) {
- tokens.push_back(s.substr(start, end - start));
- start = end + 1;
- }
- tokens.push_back(s.substr(start));
- return tokens;
- }
- void add(string questions_arr[], int size, string login) {
- ifstream file;
- file.open("estimation/progress.txt");
- int all_lines = 0;
- if (file.is_open()) {
- ofstream temp_file;
- temp_file.open("estimation/progress_temp.txt", std::ios::app);
- string questions_to_replace = "";
- if (temp_file.is_open()) {
- string line;
- while (getline(file, line)) {
- all_lines++;
- if (login == line) {
- temp_file << line << '\n';
- string questions;
- getline(file, questions);
- cout << "q to replace " << questions << endl;
- questions_to_replace = questions;
- string new_questions = "";
- for (int i = 0; i < size; i++) {
- questions = questions + "@" + questions_arr[i];
- }
- temp_file << questions << '\n';
- }
- else {
- temp_file << line << '\n';
- }
- }
- file.close();
- ofstream file2("estimation/progress.txt", ofstream::trunc);
- file2.close();
- ofstream origfile("estimation/progress.txt", std::ios::app);
- temp_file.close();
- ifstream temp_file_r("estimation/progress_temp.txt");
- string temp_line;
- int curr_line = 0;
- while (getline(temp_file_r, temp_line)) {
- if (curr_line == all_lines) {
- cout << "-2 line is " << temp_line;
- origfile << temp_line;
- }
- else {
- origfile << temp_line << '\n';
- }
- curr_line++;
- }
- temp_file.close();
- temp_file.open("estimation/progress_temp.txt", ofstream::trunc);
- temp_file_r.close();
- origfile.close();
- }
- }
- else {
- std::cout << "Unable to open file.\n";
- }
- }
- string login = "loginnewone356";
- ifstream file("estimation/progress.txt");
- string str;
- int curr = 0;
- bool user_was_not_found = true;
- while (getline(file, str)) {
- if (login == str) {
- user_was_not_found = false;
- string ques;
- getline(file, ques);
- if (ques == "") {
- cout << "Questions for " << login << " are empty" << endl;
- }
- else {
- cout << "Questions for " << login << endl;
- vector<string> qs = split_string_lol(ques, '@');
- for (const string& q : qs) {
- if (q != "") {
- cout << "q is " << q << endl;
- }
- }
- }
- }
- curr += 2;
- }
- if (user_was_not_found) {
- std::ofstream outfile;
- outfile.open("estimation/progress.txt", std::ios::app); // open file in append mode
- if (outfile.is_open()) {
- outfile << "\n";
- outfile << login << "\n";
- outfile.close();
- }
- else {
- std::cout << "Unable to open file.\n";
- }
- }
- string questions[3];
- questions[0] = "addresses:5";
- questions[1] = "addresses:7";
- questions[2] = "addresses:35";
- add(questions, 3, login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement