我正在寫一個比例計算器。在程序開始時,它從相同文件夾中的.txt加載ASCII文本藝術圖片。如何「選擇」當前目錄?
這裏是我正在做它:
//Read picture
string line;
ifstream myfile("/Users/MYNAME/Desktop/MathScripts/Proportions/Value Finder/picture.txt");
if (myfile.is_open()) {
while (!myfile.eof()) {
getline(myfile, line);
cout << line << endl;
}
myfile.close();
} else cout << "Unable to load picture!!!" << endl;
//Finish reading txt
我聽說如果.TXT是在同一文件夾,你可以只使用名稱,而不是不得不說的目錄如何。含義而不是
/Users/MYNAME/Desktop/MathScripts/Proportions/Value Finder/picture.txt
我只能使用「picture.txt」。這對我不起作用,我希望用戶能夠移動「Value Finder」文件夾而無需編輯任何代碼。
我在Mac和我正在使用CodeRunner;奇怪嗎?
請不要告訴我,以確保picture.txt是在同一文件夾作爲我的代碼。它是。
你確定你有你的路徑上的當前目錄? – kronion 2013-04-20 18:42:47
圖像正在加載。我只是想使用純picture.txt而不是/用戶/ MYNAME /桌面/ MathScripts /比例/值查找/ picture.txt – user2302825 2013-04-20 18:46:56
picture.txt並不一定是您的代碼所在的文件夾中。如果您不想指定路徑,它需要位於運行代碼的文件夾中。 – Mat 2013-04-20 18:54:27