Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode App No Longer Reads Input From the Folder The App is Stored In

The title is a bit long-winded, but basically, I've written an app that reads and writes its input and output to text files. The entire time, it would read and write the files directly in the same directory as my Xcode derived data->project->build->products->debug folder. This was where everything was being written to and read from. I don't have a custom path set up for the application, so it just saves wherever the app is located. For the first time ever, I ran Apple's Instruments app, to try to learn how to use a profiler. Not long after selecting this app as the target in Instruments, I went back to the Xcode app to run the program some more. Everything works fine in Xcode. It reads from the files and prints to files in the same location as the folder, but if I try to run the actual program itself by clicking on the file and having it open terminal, it no longer reads or prints to the directory that app is in. Instead, its printing and reading from my home folder. I don't know what changed or what caused it to change, but I'm hoping its a simple fix. I'd like for the application to read from files and print files from the directory its located in again. I'm not sure if its an Xcode setting or a Terminal setting.

Any help would be greatly appreciated.

Update 1: Tried this with no luck:

how to change the working directory to the location of the program

The directory field was blank, so I thought this would the solution, but filling it in with the suggestion did nothing to alleviate the issue.

Update 2:

Just tried deleting the preference file, still no solution. I'm willing to give someone reputation. I don't have a whole lot because I'm a newer member, but I'll give what the person thinks is fair, to whoever solves it. I'm desperate and really don't want to wait 2 days to have this issue solved.

Update 3:

Tried changing the default path in the "Profile (release)->options area in the scheme section to the default variable suggested in update 1. No luck. I'm beginning to lose my mind.

Update 4:

I've tried deleting the scheme entirely and making a new one, in hopes that maybe there was something botched with the scheme, but this did not solve the issue. Input and output while running the app in Xcode is still using the working directory, while running the executable in the debug folder is using the home folder.

Update 5:

Just tested this on an older iMac and Xcode setup (OS 10.8.5 and Xcode 5.1.1) and it seems to be working correctly, reading and writing to the current working directory of the application in the debug folder.

like image 448
joe_04_04 Avatar asked Dec 01 '16 21:12

joe_04_04


1 Answers

For whatever reason, the solution suggested by https://stackoverflow.com/a/15537436/1035008 no longer works. Maybe broken in Xcode 8.1. But this seems to work:

#include <iostream>
#include <fstream>
#include <unistd.h>

using namespace std;

int main (int argc, const char * argv[])
{
    // argv[0] returns the full path to the program, in my case "/Users/yuchen/Library/Developer/Xcode/DerivedData/../Debug/test
    string directory(argv[0]);
    // And we want to get rid of the program name `test`
    directory = directory.substr(0, directory.find_last_of("/"));
    // Point the directory to the program directory
    chdir(directory.c_str());

    cout << "Current directory is: " << getcwd(NULL, 0) << endl; // /Users/yuchen/Library/Developer/Xcode/DerivedData/../Debug/

    ifstream fin("hi.txt");
    if (fin.is_open()) cout << "File is Open" << endl;
    else cout << "File is not open" << endl;
    fin.close();

    return 0;
}

Also see SO and SO. Hope this helps.

like image 147
Yuchen Avatar answered Nov 17 '22 21:11

Yuchen