フルパス(full-path/roots.txt)を書き込むとファイルが開きます。 ファイル名(roots.txt)のみを書き込むとファイルが開けませんOpenメソッドでフルパスのみを開くC++
さらに、roots.txtはmain.cppと同じフォルダにあります。 Xcodeでチェックする必要のある設定はありますか?
は、ここでは、コードです:あなたはXCodeの内からファイルを実行していると仮定すると、
string line;
ifstream infile;
infile.clear();
// infile.open("roots.txt");
infile.open("/Users/programming/C++/roots/roots.txt");
if (infile.fail()) cout << "could not open the file: " << strerror(errno);
getline(infile, line);
cout << line;
作業ディレクトリを参照してください。 http://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode – birryree