Reputation: 366
I'm making a project in Xcode and I tried to mix logic written in C++ and handling of GUI components in Objective-C. I came across the following problem:
Whenever I open a file stream in C++ code (as I usually do in projects written completely in C++ and it DOES work), the file couldn't be opened and I got no error message and no clue how to handle the situation. The following code lies in a method of my C++ class, there's nothing wrong with it:
std::fstream file;
file.open("file");
if (file) {
file << "The string that is not printed to a file";
}
file.close();
What can do to fix file streams and make them work in a mixed-code project?
Note: the question is not about handling both C++ and Objective-C in a single project, as I can call C++ methods successfully with a wrapper object.
Upvotes: 1
Views: 57
Reputation: 366
Indeed, the problem comes from the fact that Cocoa applications work inside their own sandbox and cannot by default write to or read from any file outside it.
I solved the problem by preliminarily creating a file and specifying the absolute path to it. I managed to find out the path of the sandbox with the help of getcwd()
C standard library function:
char cwd[100];
std::string sandbox_path = getcwd(cwd, sizeof(cwd));
std::string full_path = sandbox_path + "/" + FILE_NAME;
std::fstream source_file;
source_file.open(full_path, std::ios::in | std::ios::out | std::ios::binary );
Thank you to those who replied!
Upvotes: 1