我在 Stack Overflow 上查看了很多有关 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;
}
我已确认通过检查
argv[0]
的值,input.txt文件位于正确的“工作目录”中。但是Open方法无法正常工作。我还遇到了调试问题-我不能设置在
infile.good()
或infile.is_open()
上监视吗?我一直收到以下错误信息:Error: member function not present.
编辑:更新了包含完整代码的.CPP文件的代码清单。
更新:该文件不在当前工作目录中。这是项目文件所在的目录。将其移动到该目录中,在VS.NET中调试时它可以正常工作。