如果流和流在崩溃后不起作用
ifstream and ofstream not working after crash
我第一次尝试 c++,我想我会做一个小程序,只打印出文件中的行。我正在使用 Clion IDE,一切正常,它工作正常。然后我的电脑突然冻结了,当我尝试再次运行代码时,ifstream 似乎没有打开。代码如下:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("hello.txt");
cout << file.is_open() << endl;
string line;
while(getline(file, line)) cout << line << endl;
return 0;
}
我尝试重新安装cygwin(可能没有正确完成,不知道)和Clion,但没有帮助。
编辑:尝试通过网站编译代码,它可以工作,但是当我在我的机器上运行它时,文件无法打开。
编辑2:Clion在捉弄我并更改了工作目录,再次设置后一切正常。解决
//Don't forget to include fstream
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("hello.txt");
if(file.is_open())
{
string line;
while(!file.eof()) //while we are not yet to the end of the file
{
getline(file, line)
cout << line << endl;
}
}
else
cout<<"File not opened n";
return 0;
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- 在C++中实现二叉搜索树,搜索不起作用.尝试打印节点的元素会导致输出崩溃
- 使用 XMMatrixLookAtLH - 试图面对一个点,但不起作用/崩溃?
- 如果流和流在崩溃后不起作用
- try() 和 catch() 不起作用;程序崩溃,从不执行 catch() 块
- C++ 排序会崩溃,而stable_sort不会,但两者都不起作用