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

Ogh*_*ris 5 c++ ifstream

好吧,这是我第一次在Xcode中编写C++(我习惯于ObjC),现在我已经开始在我的大学开设编程课程.

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

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

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

//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
Run Code Online (Sandbox Code Playgroud)

如果我正在做其他事情,请告诉我,因为我确信这总是很有可能:)

ssu*_*ube 18

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

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

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

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

这应该在控制台中显示它.该getcwd功能有一些变化取决于你运行的,我没有在Mac上测试,但信息在这里:

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


小智 5

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

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

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