gpt4 book ai didi

XCode : Where is the default directory? 上的 C++ ifstream

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:11 28 4
gpt4 key购买 nike

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

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

我假设这是因为我拥有的 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

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

最佳答案

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

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

要获取当前目录(查看),getcwd() C 函数(也可在 C++ 中使用)会有所帮助。像这样的东西:

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

这应该会在控制台中显示它。 getcwd 函数有一些变化,具体取决于您运行的是什么,我没有在 Mac 上测试过,但这里有信息:

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

关于XCode : Where is the default directory? 上的 C++ ifstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7279100/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com