Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- //takes in an fstream, a filename, two ints
- void temp(std::fstream &fs, const std::string &filename, int count)
- {
- using namespace std;
- int start, delta;
- //input start
- cout << "Input the start point of deletion. Starts at 1, ends at " << count << endl;
- cin >> start;
- if(start < 1 || start > count) { cout << "You died" <<endl; return; }
- //input delta
- cout << "Input how many messages you want to delete from the start point;"
- << " Write 1 to delete only the one at the start; 0 to not delete anything; max value is " << count+1-start << endl;
- cin >> delta;
- if(delta < 0 || delta > count+1-start) { cout << "You died" << endl; return; }
- if(delta == 0) return;
- std::string fs_filename = filename + "_temp.txt"
- std::string line;
- while(getline(fs, line)) if(line.find('\r') != std::string::npos) line.erase(line.end()-1, line.end());
- fs.clear(); fs.seekg(0);
- if(!fs.is_open())
- {
- std::cout << "error" << std::endl;
- return;
- }
- auto print_ = [&fs]()
- {
- int counter = 0; fs.clear(); fs.seekg(0);
- std::string line;
- while(getline(fs, line)) std::cout << ++counter << ". "<< line << '\n';
- fs.clear(); fs.seekg(0);
- };
- //print_();
- //make a fstemp file
- std::fstream fstemp(fs_filename, std::fstream::out);
- int counter(0);
- fstemp.close(); fstemp.open(filename, std::fstream::in | std::fstream::out);
- while(getline(fs, line)) {
- if(++counter >= line_to_delete)
- fstemp << line << '\n';
- }
- fs.close(); fs.open(filename, std::fstream::out);
- while(getline(fstemp, line))
- {
- //std::cout << line;
- fs << line;
- //STILL couts nothing
- }
- //print_(); //prints nothing
- //test_temp.txt is empty after running it
- //test.txt has expected output
- std::remove(fs_filename.c_str());
- return;
- }
- int main()
- {
- std::string filename = "test";
- filename += ".txt";
- std::fstream fs(filename, std::fstream::in | std::fstream::out);
- temp(fs, filename);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement