2016-04-14 56 views
0

我是一名初學C++程序員,之前習慣於使用Visual Studio,但現在我正在使用mac,現在我使用xcode。在xcode中,我無法從文件中讀取數據(所以我也無法獲得輸出)。我應該在xcode中更改哪個設置以從文件讀取數據(該文件與項目位於同一文件夾中)正確嗎?無法從xcode中的文件讀取數據

#include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 
//in thefile.txt i only have a string "hello" 
int main() { 
    string text; //since i only have a string in the file. So this is a variable representation for it 
    ifstream myfile; 
    myfile.open("thefile.txt"); 
    myfile >> text; //extract the word from the file 
    cout << "The following is the data in the file: " << text <<endl; //trying to print The following is the data in the file: hello 
    return 0; 
} 
//Output is "The following is the data in the file: " 

回答

0

也許最直接的方法是使用完全合格的文件名來猜測出它。如果您不確定全名,可以在OS X Terminal程序中打開一個窗口,輸入「file」,然後將您的文件從Finder窗口拖到該命令行上。終端將填寫完整的路徑。 「文件」是一個無害的命令,會告訴你關於你的文件類型的信息。但重要的是,您將擁有完整的路徑,然後您可以從終端複製並粘貼到您的代碼中。

如果您的文件路徑中有空白,您可能需要手動刪除將出現的反斜槓字符。

Xcode本身沒有一個設置(我知道)來確定啓動程序時哪個目錄處於活動狀態。但是,一條完整的道路會將猜測排除在外。

+0

謝謝。它有幫助。我還通過xcode(產品>方案>編輯方案>選項(內部運行))找到了一種方法,然後將目錄切換到您的代碼所在的目錄。 –