Xcode 应用程序不再从存储应用程序的文件夹中读取输入 [英] Xcode App No Longer Reads Input From the Folder The App is Stored In

查看:45
本文介绍了Xcode 应用程序不再从存储应用程序的文件夹中读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题有点冗长,但基本上,我已经编写了一个应用程序,可以将其输入和输出读取和写入文本文件.在整个过程中,它会直接在与我的 Xcode 派生 data->project->build->products->debug 文件夹相同的目录中读取和写入文件.这是所有东西都被写入和读取的地方.我没有为应用程序设置自定义路径,所以它只是保存应用程序所在的位置.我有史以来第一次运行 Apple 的 Instruments 应用程序,尝试学习如何使用分析器.在 Instruments 中选择这个应用程序作为目标后不久,我又回到了 Xcode 应用程序来运行更多的程序.在 Xcode 中一切正常.它从文件中读取并打印到与文件夹位于同一位置的文件,但是如果我尝试通过单击文件并打开终端来运行实际程序本身,它不再读取或打印到应用程序所在的目录相反,它从我的主文件夹中打印和读取.我不知道发生了什么变化或导致它发生变化的原因,但我希望这是一个简单的修复.我希望应用程序从文件中读取并再次从其所在目录中打印文件.我不确定它是 Xcode 设置还是终端设置.

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.

任何帮助将不胜感激.

更新 1:尝试了这个但没有运气:

Update 1: Tried this with no luck:

如何改变工作目录到程序所在的位置

目录字段是空白的,所以我认为这是解决方案,但用建议填充它并没有缓解问题.

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.

更新 2:

刚刚尝试删除首选项文件,仍然没有解决方案.我愿意给某人声誉.我没有很多,因为我是一个新成员,但我会把这个人认为公平的东西交给解决它的人.我很绝望,真的不想等 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.

更新 3:

尝试将方案部分中配置文件(发布)->选项"区域中的默认路径更改为更新 1 中建议的默认变量.不走运.我开始失去理智.

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.

更新 4:

我已经尝试完全删除该方案并创建一个新方案,希望该方案可能存在一些问题,但这并没有解决问题.在 Xcode 中运行应用程序时的输入和输出仍然使用工作目录,而在调试文件夹中运行可执行文件时使用主文件夹.

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.

更新 5:

刚刚在较旧的 iMac 和 Xcode 设置(OS 10.8.5 和 Xcode 5.1.1)上对此进行了测试,它似乎工作正常,读取和写入调试文件夹中应用程序的当前工作目录.

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.

推荐答案

无论出于何种原因,https://stackoverflow 建议的解决方案.com/a/15537436/1035008 不再有效.也许在 Xcode 8.1 中坏了.但这似乎有效:

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;
}

另见SOSO.希望这可以帮助.

Also see SO and SO. Hope this helps.

这篇关于Xcode 应用程序不再从存储应用程序的文件夹中读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆