Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fstream does not save changes C++

I made a program that is supposed to be a simple text editor that can create (if not existed) a .cpp file, read the content of the file and display it on the console, and modify the content and save the changes when Ctrl+S is pressed.

The program does all the above, except saving the changes. Can anybody explain what the problem is?

Here is below the full code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>
#include <fstream>
#include <filesystem>

namespace fs = std::filesystem;
void work(std::fstream& file, const std::string& path)
{
    std::string previous;
    while (not file.eof()) {
        std::getline(file, previous);
        if (not previous.empty())
            std::cout << previous << '\n';
    }
    char c;
    std::string buff;
    while (true) {
        c = std::cin.get();
        buff += c;
        switch (c) {
            case 19:
                if (not file.is_open()) {
                    file.open(path, std::ios::in | std::ios::out);
                    if (not file.is_open()) {
                        std::cerr << "Unable to open!" << std::endl;
                        exit(EXIT_FAILURE);
                    }
                }
                file.seekp(0, std::ios::end);
                buff.pop_back();
                for (char x : buff)
                    file.put(x);
                std::cout << "Saved!" << std::endl;
                buff.clear();
                file.close();
                break;
            default:
                continue;
        }
    }
}

int main()
{
    const std::string path = "D:\\some_file.cpp";
    if (not fs::exists(path)) {
        std::ofstream new_file(path);
        if (not new_file.is_open()) {
            std::cerr << "Unable to create the file!" << std::endl;
            return EXIT_FAILURE;
        }
        else
            new_file.close();
    }
    std::fstream file(path, std::ios::in | std::ios::out);
    work(file, path);
}

>Solution :

At the end of the first while loop in work, file must be in the eof state (or worse). If a stream has any flags set then all subsequent operations on the stream will immediately fail and do nothing. To return the stream to a working state you need to call file.clear().

Also note that while (not file.eof())) is usually incorrect as it will read an extra line. You should do this instead:

while (std::getline(file, previous)) {
    std::cout << previous << '\n';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading