当文件位于正确位置时,ifstream无法打开该文件
ifstream fails to open a file when it is in the correct location
我正在像这样打开文件…
ifstream file("testing.txt", ios::in);
if (file.is_open()) {
cout << "opened!";
}
else {
cout << "failed!";
}
文件位置如下。。。source -> repos -> TempStuff -> FirstStuff -> testing.txt
我的解决方案是TempStuff
,cpp文件是FirstStuff
。文件没有打开,我不知道为什么。
请将您的路径更改为绝对路径。当前您的文件路径为/testing.txt。在当前工作目录(cpp文件可用的位置(中搜索文件testing.txt通过将您的路径更改为绝对路径(./source/reso/TempStuff/FirstStuff/testing.txt(,您将打开您的文件。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream file("./source/repos/TempStuff/FirstStuff/testing.txt", ios::in);
if (file.is_open()) {
cout << "opened!";
}
else {
cout << "failed!";
}
}
-
使用文件的绝对路径来确保权限不是问题的原因。
-
如果使用绝对路径有效,则程序的启动目录与文件所在的目录不同。更新IDE设置中的启动目录。
相关文章:
- ifstream文件在从行中读取时被清除
- 是否可以在没有真实文件的情况下创建 ifstream
- 虽然循环不读取整个 ifstream 文件
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- ifstream 读取的字符与文件中写入的字符不同
- 如何使用 ifstream 检查文件中变量的类型?
- 尝试使用 Ifstream 打开.txt文件时C++问题
- ifstream不会在下一次迭代中打开文件
- 当文件位于正确位置时,ifstream无法打开该文件
- 使用ifstream时,被调用文件中的数据不会打印
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- ifstream说它打开了一个文件,但文件没有打开
- VS Express 2015 Win10 应用程序 - ifstream 无法打开文件
- C++ : 如何在使用 getline() 和 ifstream 对象从文件中读取一行时跳过第一个空格?
- C++如何附加ifstream以将多个文件读取为一个大文件
- 尝试在c++中将文本文件(ifstream)转换为2D字符数组
- 读取文件(ifstream)和写入文件(ofstream)-编码问题
- 如何共享文件删除权限时,我打开一个文件ifstream