开发者

Can't read file from a C++ program on a Mac

First of all, this is part of my code:

....
string input;
getline(cin, input);

ifstream openFile;
openFile.open(input.c_str(), ios::in);

if(openFile.is_open()){
    cout << "File opened" << endl;
}  
else {
    cout << "Cant open the file " << endl;
}

The result always "Cant open the file". I am very, very sure that the files are exists. I have data1.txt, data2.txt ... data10.txt in the same directory (I used XCode to add new empty file, add the data inside and save it).

I do ano开发者_运维问答ther test, I create a new directory, copy paste the cpp and data files. I run in terminal, and it works, it can read the data file. Why does xcode cant read my data files? Any idea?


You need to give the full path to the files. Xcode will run the application from the build directory which is not where the code is.

If the files are copied as part of building an OSX or iOS application you should look at the bundle structure to find the directory.


You can tell Xcode to run the executable from the directory containing the data files.

Bring up the info dialog for the target executable, and change the value for 'Set the working directory to:' to either the Project directory or a custom directory'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜