英文:
fstream does not save changes C++
问题
我制作了一个应该是一个简单文本编辑器的程序,它可以创建(如果不存在)一个.cpp
文件,读取文件的内容并在控制台上显示它,以及在按下<kbd>Ctrl+S</kbd>时修改内容并保存更改。
该程序可以完成上述所有操作,除了保存更改。有人可以解释一下问题在哪里吗?
下面是完整的代码:
#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 << "无法打开文件!" << std::endl;
exit(EXIT_FAILURE);
}
}
file.seekp(0, std::ios::end);
buff.pop_back();
for (char x : buff)
file.put(x);
std::cout << "已保存!" << 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 << "无法创建文件!" << 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 <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);
}
答案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 << previous << '\n';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论