-1

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:

#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);
}
7
  • 3
    Probably unrelated: Watch out for while(not file.eof()) Commented Jul 10, 2023 at 19:20
  • 3
    Side note: Never post the full code. Post a minimal reproducible example. Odds are really good that you'll find the mistake while making the minimal example and not have to ask the question. Commented Jul 10, 2023 at 19:21
  • Recommendation: In case 19: without context 19 is meaningless. You want to use terms that describe what's really happening in the code and what the 19 stands for. Commented Jul 10, 2023 at 19:25
  • @user4581301 it is actually the ascii code for ctrl+s Commented Jul 10, 2023 at 19:26
  • 1
    What do you observe when you press Ctrl+S? Commented Jul 10, 2023 at 19:41

1 Answer 1

3

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';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.