我正在尝试使用ifstream处理一些文件。一切看起来都很好,但是当我尝试打开一个文件时,它不起作用。无论我是否尝试输入字符串变量或字符串文字作为名称。我尝试访问的文件与项目位于同一目录中,并且包含内容。ifstream在Xcode中不起作用?
该项目不显示任何错误,并将编译,但它只会说它不能每次访问该文件。
附加标题“simpio.h”和“console.h”只是斯坦福提供的库。
#include <iostream>
#include "console.h"
#include "simpio.h"
#include <fstream>
#include <string>
using namespace std;
int countLines(ifstream & in)
{
int count = 0;
while (true) {
string line;
getline(in, line);
if (in.fail()) break;
count++;
}
return count;
}
int main()
{
ifstream in;
while (true) {
cout << "Enter name: ";
string s = getLine();
in.open(s.c_str());
if (!in.fail()) break;
cout << "Couldn't open file, try again!" << endl;
in.clear();
}
cout << "Num lines = " << countLines(in) << endl;
return 0;
}
任何帮助将是伟大的!谢谢!
您是否在与您要查找的文件相同的目录中启动该程序? – 2013-03-22 17:27:04
你是什么意思?程序和所有文件都在同一个项目文件夹中。 – Jonathan 2013-03-22 17:28:24
'ifstream'在Xcode中肯定不起作用。它可以在标准库,程序等中使用。类提供的功能仅取决于类,而不取决于IDE,天气或邻居猫的当前情绪...... – 2013-03-22 17:29:18