我正在嘗試使用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