XCode 上的 C++ ifstream:默认目录在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7279100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:43:49  来源:igfitidea点击:

C++ ifstream on XCode: Where is the default directory?

c++ifstream

提问by OghmaOsiris

Ok, so this is the first time I've coded C++ in Xcode (I'm used to ObjC)and I've now started a programming course at my college.

好的,这是我第一次在 Xcode 中编写 C++ 代码(我已经习惯了 ObjC)并且我现在已经在我的大学开始了一门编程课程。

I'm trying to open a file (either hard coded or from user input in the console) and no matter what I try, it says the file won't open (through error checking)

我正在尝试打开一个文件(硬编码或来自控制台中的用户输入),无论我尝试什么,它都说该文件无法打开(通过错误检查)

I'm assuming it's because the test.txt file I have isn't in the assumed root directory, so if that's the case, what is the root directory?

我假设这是因为我拥有的 test.txt 文件不在假定的根目录中,所以如果是这种情况,根目录是什么?

Here's my code so far:

到目前为止,这是我的代码:

//include files
#include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;

//Global Variables
short inputPicture[512][512];
short outputPicture[512][512];

//Function Prototypes
void getInput(char* in, char* out);
void initializeArray(ifstream* input);

//Main
int main(){
    //local variables
    char inputFile[32];
    char outputFile[32];

    ifstream input;
    ofstream output;


    getInput(inputFile, outputFile);
    cout << inputFile << endl;//test what was sent back from the function

    input.open(inputFile, ifstream::in);
    if (!input.is_open()){//check to see if the file exists
        cout << "File not found!\n";
        return 1;//if not found, end program
    }

    initializeArray(&input);

    return 0;
}//end Main

//Gets initial input from user
void getInput(char* in, char* out){
    cout << "Please designate input file: ";
    cin >> in;
    cout << "\nPlease designate an output file: ";
    cin >> out;
}//end getInput


//Sets the global array to the information on the input file
void initializeArray(ifstream* input){



}//end initializeArray

Please let me know if there's something else wrong I'm doing, as I'm sure that's always a great possibility :)

如果我做错了什么,请告诉我,因为我相信这总是很有可能的:)

回答by ssube

The default directory should be relative the application's working directory, which is usually the same place the application is located (debuggers can mess with that, sometimes).

默认目录应该是相对于应用程序的工作目录,它通常与应用程序所在的位置相同(有时调试器可能会弄乱它)。

For simple testing, just specify an absolute path in the command line (or code).

对于简单的测试,只需在命令行(或代码)中指定一个绝对路径即可。

To get the current directory (to see), the getcwd()C function (also usable in C++) will help. Something like:

要获取当前目录(查看),getcwd()C 函数(也可在 C++ 中使用)将有所帮助。就像是:

char * dir = getcwd(NULL, 0); // Platform-dependent, see reference link below    
printf("Current dir: %s", dir);

That should display it in the console. The getcwdfunction has a few variations depending on what you run on, I've not tested on Mac, but info here:

那应该在控制台中显示它。该getcwd功能有一些变化,具体取决于您运行的内容,我没有在 Mac 上测试过,但信息如下:

http://linux.die.net/man/3/getcwd

http://linux.die.net/man/3/getcwd

回答by Jason

The "root" directory that your executable is looking for the file in is not the actual /root directory of the file-system, but is the directory that the executable is executing in ... if you are using Xcode, this may be buried inside one of the build directories automatically created by Xcode for your project rather than a user home folder or home folder sub-directory like /Users/XXXXXX/Documents.

您的可执行文件在其中查找文件的“根”目录不是/文件系统的实际根目录,而是可执行文件正在执行的目录......如果您使用的是 Xcode,这可能被埋在里面由 Xcode 为您的项目自动创建的构建目录之一,而不是用户主文件夹或主文件夹子目录,如/Users/XXXXXX/Documents.

回答by Seth Carnegie

The "default directory" is the directory from which the executable was executed. Usually this is in the same folder as the executable, although if you do stuff like dragging and dropping files on an exe, it can change the startup path.

“默认目录”是执行可执行文件的目录。通常这与可执行文件位于同一文件夹中,但如果您在 exe 上执行诸如拖放文件之类的操作,它可以更改启动路径。

The path can also change if you're running the program from inside your IDE. The IDE starts the executable, so there's no telling where it's doing it from. You'll have to find where it stores executables and put the file in there, or use an absolute path.

如果您从 IDE 内部运行程序,路径也可能会发生变化。IDE 启动可执行文件,所以不知道它是从哪里开始执行的。您必须找到它存储可执行文件的位置并将文件放在那里,或者使用绝对路径。

回答by mahoukov

In the Xcode (v7.1.1 at the time of writing)sidebar, there's an automatically generated folder called "Products". Inside you'll find the executable of your project (assuming you've built your project at least once). Right-click it & choose "Show in Finder". A folder will open in Finder. That's the working directory of your program, & you'll notice it's not actually inside your project's folder.

在 Xcode (撰写本文时为 v7.1.1)侧栏中,有一个自动生成的文件夹,名为“Products”。在里面你会找到你的项目的可执行文件(假设你已经构建了你的项目至少一次)。右键单击它并选择“在 Finder 中显示”。一个文件夹将在 Finder 中打开。那是您程序的工作目录,您会注意到它实际上并不在您的项目文件夹中。

You can have Xcode use a different directory instead. In the top toolbar, on the left side where it shows your project name next to the active build architecture, click on the Project name > Edit Scheme…

您可以让 Xcode 使用不同的目录。在顶部工具栏中,在活动构建架构旁边显示您的项目名称的左侧,单击项目名称 > 编辑方案...

Then look for an option called "Working Directory" in the sheet that appears. Tick the checkbox & then choose a custom directory. Note: Make sure the "Run" option is the selected one in that sheet's sidebar.

然后在出现的工作表中查找名为“工作目录”的选项。勾选复选框,然后选择一个自定义目录。注意:确保“运行”选项是该工作表侧栏中的选定选项。

回答by shinya

In my case, - getcwd(NULL, 0) returned "/". - and couldn't use abusolute path.(It changes on each terminal deployed.)

就我而言, - getcwd(NULL, 0) 返回“/”。- 并且不能使用绝对路径。(它在每个部署的终端上发生变化。)

So I got the path by ObjC code and throw it to c++ function.

所以我通过ObjC代码得到了路径并将其扔给c++函数。

1.Put files in top directory of xcode project. And check they are included in "Targets"->"Build Phases"->"Copy Bundle Resources".

1.将文件放在xcode项目的顶级目录中。并检查它们是否包含在“目标”->“构建阶段”->“复制捆绑资源”中。

2.Get the path by ObjC code.

2.通过ObjC代码获取路径。

NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString: @"/"];

3.And throw it to c++ function.

3.并把它扔给c++函数。

[self cppFunc:[path UTF8String]];

4.Then you can make an ablsolute file path in c++.

4.然后你可以在c++中制作一个绝对的文件路径。

std::string file = path(arg) + "filename";

It worked for me.

它对我有用。