2011-12-23 62 views
0

最好的方式來解釋我的問題可能只是給你看我的代碼,因爲它是因爲它得到簡單。問題對象

#include <iostream> 
#include <fstream> 

int main (int argc, const char * argv[]) 
{ 

    std::ifstream in; 
    std::string line; 
    in.open("test.txt"); 
    if (in.fail()) std::cout << "failed. \n"; 
    getline(in, line); 
    std::cout << line; 

    return 0; 
} 

所以當我運行這個時,控制檯返回「失敗」。而不是打開名爲test.txt的文件 - 它與我的.xcodeproj文件位於同一文件夾中,並且也顯示在我的Xcode導航器中。

我不知道我誤解這個過程,但我懷疑這將是一些簡單。

謝謝! :)

回答

3

的文件在同一目錄中.xcodeproj文件?那麼,你的問題就在這裏。

默認情況下,從Xcode中啓動的進程的工作目錄將在輸出目錄(即程序所在的目錄)。根據您的Xcode版本,它可能會在<Project Directory>/build/Debug

嘗試在那裏移動文件。

+1

+1。 'ifstream :: open()';-)沒什麼問題 – Cameron 2011-12-23 04:41:29

+0

嗚呼!這是解決方案,謝謝! 文件夾名最終爲: /Users/username/Library/Developer/Xcode/DerivedData/project_dir/Build/Products/Debug – Jarrod 2011-12-23 04:47:51

+0

Thanks Cam,good tips :) – Jarrod 2011-12-23 04:49:59