2

I am using XCode, and here is a c++ code in XCode

std::fstream stream("templates.Xml", std::ios::binary | std::ios::in);
if(!stream) return false;

enter image description here

I put the Xml file in the folder that contain the ".xcodeproj", and I put it in the folder that contains ".app" but the stream always return false, why?

Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41
  • Because running your app from Xcode doesn't use the current working directory you think it does. you can manually set it in the Scheme settings, however, or include the file *in* your project (not just the directory, but actually add it to the project itself as a file). I prefer the former, as I like knowing where my programming is running at all times (which Macs don't always make apparent). – WhozCraig Feb 21 '14 at 11:45
  • 2
    Please see the image, I edit the question, but it's not working too. – Hazem Abdullah Feb 21 '14 at 11:54
  • Yeah, I come to find out that 5.0.2 that only works with libs now. You're going to have to set the working dir in the scheme. Its pretty straight forward. [See this question and answer](http://stackoverflow.com/questions/14476655/code-runs-perfect-in-g-but-not-in-xcode-cannot-find-file/14478210#14478210) – WhozCraig Feb 21 '14 at 11:58
  • 2
    I could not see "Working Directory" http://tinypic.com/view.php?pic=15dl0dc&s=5#.UwhzbfmSyP4 – Hazem Abdullah Feb 22 '14 at 10:03
  • That would be because you're running the ios simulator as a project runtime config. I'm not even sure it is possible in that light. maybe, but chances your data file will need deployment it it is. For console OSX applications its trivial (obviously). – WhozCraig Feb 22 '14 at 10:09

2 Answers2

3

It looks like you are trying to open the file from the wrong directory. "templates.Xml" is saved in the bundle- is not saved in the documents directory. By default, if you open "./filename", this actually points to:

/Users/arinmorf/Library/Application Support/iPhone Simulator/7.0.3/Applications/246E91F9-FAB2-4A46-B1F1-855B5363F24D/Documents/

Where arinmorf would be your username and the long hex string is randomly generated every time you install the app on the simulator.

The templates.xml file would be found in: /Users/arinmorf/Library/Application Support/iPhone Simulator/7.0.3/Applications/246E91F9-FAB2-4A46-B1F1-855B5363F24D/iFly.app/templates.xml

iFly.app is the name of my app, yours would be "T". BUT you can't use the absolute path in your project, because of the randomly generated string, you need to use the NSBundle or CFBundleRef.

In objective-C, you would use:

filePath = [[NSBundle mainBundle] pathForResource:@"templates" ofType:@"xml"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

In C++ it looks like:

CFURLRef fileURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("templates"), CFSTR("xml"), NULL);
CFStringRef filePath = CFURLCopyFileSystemPath(fileURL, kCFURLPOSIXPathStyle);
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
const char *path = CFStringGetCStringPtr(filePath, encodingMethod);
FILE* f = fopen(path, "r");

(Credit to Chris Frederick at https://stackoverflow.com/a/8768366/2070758 for C++ version)

Community
  • 1
  • 1
arinmorf
  • 1,374
  • 16
  • 27
0

Yes, I solved the problem in two ways either depending on Main Bundle, or create custom bundle and use it.

Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41