我一直在讨论关于 SO 的 ifstream 问题,但我仍然无法阅读一个简单的文本文件。我正在使用 Visual Studio 2008。
这是我的代码:
// CPPFileIO.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
infile.open("input.txt", ifstream::in);
if (infile.is_open())
{
while (infile.good())
cout << (char) infile.get();
}
else
{
cout << "Unable to open file.";
}
infile.close();
_getch();
return 0;
}
通过检查 .txt 的值,我确认input.txt文件位于正确的“工作目录”中argv[0]
。Open 方法是行不通的。
我在调试时也遇到了麻烦-我应该无法设置手表吗infile.good()
?infile.is_open()
我不断得到
Error: member function not present.
编辑:使用 .CPP 文件中的完整代码更新代码列表。
更新:该文件不在当前工作目录中。这是项目文件所在的目录。把它移到那里,它在 VS.NET 中调试时工作。