我一直在这里关于ifstream的问题,所以我仍然无法阅读简单的文本文件。我与Visual Studio 2008年的工作ifstream :: open在Visual Studio调试模式下不工作
这里是我的代码:
// 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;
}
我已经证实,input.txt中文件是正确的“工作目录”通过检查argv[0]
值。 Open方法不起作用。
我也遇到了调试问题 - 我不能在infile.good()
或infile.is_open()
上设置手表吗?我不断收到
Error: member function not present.
编辑:从.cpp文件中完整的代码更新的代码清单。
更新:该文件不在当前工作目录中。这是项目文件所在的目录。将它移到那里,它在VS.NET中调试时工作。
其工作目录是你的问题。当你从命令行运行它时,工作目录是显而易见的。当你在调试器中运行它时,你需要明确地设置工作目录,否则它不是那么明显(检查调试器选项)。 – 2009-04-29 00:41:49
感谢您发布更新部分。自从我需要考虑工作目录处于调试模式的位置以来,这已经过去了一分钟。为我节省了几分钟的头部划伤。 – 2014-06-25 01:55:38