【发布时间】:2014-08-31 18:11:01
【问题描述】:
我正在尝试在 C++(Visual Studio)中打开一个简单的 txt.file,但只是触发了“else”。 code.txt 与源文件中的主文件一起包含在内。这或多或少是这样的
#include <iostream>
#include <fstream>
int main()
{
std::ifstream file("codes.txt");
if (file.is_open())
{
std::cout << "success" << std::endl;
}
else
{
std::cout << "Unable to open file" << std::endl;
}
}
【问题讨论】:
-
如果您在任何编辑器中打开了文件,请尝试关闭它并再次运行程序。代码看起来正确。另外,请检查文件是否在运行程序的目录中。
-
你有什么经验吗?我必须做的不仅仅是用 txt 文件“添加现有对象”。喜欢在节目中提及它?