fstream在C++中不保存更改。

huangapple go评论57阅读模式
英文:

fstream does not save changes C++

问题

我制作了一个应该是一个简单文本编辑器的程序,它可以创建(如果不存在)一个.cpp文件,读取文件的内容并在控制台上显示它,以及在按下<kbd>Ctrl+S</kbd>时修改内容并保存更改。

该程序可以完成上述所有操作,除了保存更改。有人可以解释一下问题在哪里吗?

下面是完整的代码:

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;filesystem&gt;

namespace fs = std::filesystem;
void work(std::fstream&amp; file, const std::string&amp; path)
{
    std::string previous;
    while (not file.eof()) {
        std::getline(file, previous);
        if (not previous.empty())
            std::cout &lt;&lt; previous &lt;&lt; &#39;\n&#39;;
    }
    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 &lt;&lt; &quot;无法打开文件!&quot; &lt;&lt; std::endl;
                        exit(EXIT_FAILURE);
                    }
                }
                file.seekp(0, std::ios::end);
                buff.pop_back();
                for (char x : buff)
                    file.put(x);
                std::cout &lt;&lt; &quot;已保存!&quot; &lt;&lt; std::endl;
                buff.clear();
                file.close();
                break;
            default:
                continue;
        }
    }
}

int main()
{
    const std::string path = &quot;D:\\some_file.cpp&quot;;
    if (not fs::exists(path)) {
        std::ofstream new_file(path);
        if (not new_file.is_open()) {
            std::cerr &lt;&lt; &quot;无法创建文件!&quot; &lt;&lt; std::endl;
            return EXIT_FAILURE;
        }
        else
            new_file.close();
    }
    std::fstream file(path, std::ios::in | std::ios::out);
    work(file, path);
}
英文:

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 <kbd>Ctrl+S</kbd> 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 &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;filesystem&gt;
namespace fs = std::filesystem;
void work(std::fstream&amp; file, const std::string&amp; path)
{
std::string previous;
while (not file.eof()) {
std::getline(file, previous);
if (not previous.empty())
std::cout &lt;&lt; previous &lt;&lt; &#39;\n&#39;;
}
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 &lt;&lt; &quot;Unable to open!&quot; &lt;&lt; std::endl;
exit(EXIT_FAILURE);
}
}
file.seekp(0, std::ios::end);
buff.pop_back();
for (char x : buff)
file.put(x);
std::cout &lt;&lt; &quot;Saved!&quot; &lt;&lt; std::endl;
buff.clear();
file.close();
break;
default:
continue;
}
}
}
int main()
{
const std::string path = &quot;D:\\some_file.cpp&quot;;
if (not fs::exists(path)) {
std::ofstream new_file(path);
if (not new_file.is_open()) {
std::cerr &lt;&lt; &quot;Unable to create the file!&quot; &lt;&lt; std::endl;
return EXIT_FAILURE;
}
else
new_file.close();
}
std::fstream file(path, std::ios::in | std::ios::out);
work(file, path);
}

答案1

得分: 3

work函数的第一个while循环结束时,file必须处于eof状态(或更糟糕的状态)。如果流上设置了任何标志,那么流上的所有后续操作将立即失败并不执行任何操作。要将流恢复到工作状态,您需要调用file.clear()

还请注意,while (not file.eof()))通常是不正确的(https://stackoverflow.com/q/5605125/4581301),因为它会多读一行。您应该改用以下方式:

while (std::getline(file, previous)) {
    std::cout << previous << '\n';
}
英文:

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 &lt;&lt; previous &lt;&lt; &#39;\n&#39;;
}

huangapple
  • 本文由 发表于 2023年7月11日 03:17:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定