C++ XCode 上的 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
如果我做错了什么,请告诉我,因为我确信这总是很有可能:)
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.
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?
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
默认目录应该是相对于应用程序的工作目录,通常与应用程序所在的位置相同(调试器有时可能会弄乱它)。
对于简单的测试,只需在命令行(或代码)中指定绝对路径即可。
要获取当前目录(要查看),
getcwd()
C 函数(也可在 C++ 中使用)会有所帮助。像这样:应该将其显示在控制台中。 getcwd 函数有一些变化,具体取决于您运行的系统,我没有在 Mac 上进行过测试,但信息如下:
http://linux.die.net/man/3/getcwd
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:That should display it in the console. The
getcwd
function has a few variations depending on what you run on, I've not tested on Mac, but info here:http://linux.die.net/man/3/getcwd
在 Xcode(撰写本文时为 v7.1.1) 侧栏中,有一个自动生成的文件夹,名为“Products”。在里面您将找到项目的可执行文件(假设您至少构建过一次项目)。右键单击它并选择“在 Finder 中显示”。将在 Finder 中打开一个文件夹。这是你的程序的工作目录,&您会注意到它实际上并不在您的项目文件夹内。
您可以让 Xcode 使用不同的目录。在顶部工具栏中,在活动构建架构旁边显示项目名称的左侧,单击项目名称 >编辑方案...
然后在出现的工作表中查找名为“工作目录”的选项。勾选复选框并然后选择一个自定义目录。 注意:确保在该工作表的侧栏中选择“运行”选项。
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.
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…
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.
您的可执行文件在其中查找文件的“根”目录不是文件系统的实际
/
根目录,而是可执行文件正在其中执行的目录...如果您是使用 Xcode,这可能会埋藏在 Xcode 自动为您的项目创建的构建目录之一中,而不是像/Users/XXXXXX/Documents
这样的用户主文件夹或主文件夹子目录。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
.“默认目录”是执行可执行文件的目录。通常,它与可执行文件位于同一文件夹中,但如果您在 exe 上执行诸如拖放文件之类的操作,它可能会更改启动路径。
如果您从 IDE 内部运行该程序,该路径也可能会更改。 IDE 启动可执行文件,因此不知道它是从哪里执行的。您必须找到它存储可执行文件的位置并将文件放入其中,或者使用绝对路径。
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.
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.
就我而言,
- getcwd(NULL, 0) 返回“/”。
- 并且无法使用 abusolute 路径。(它在部署的每个终端上都会改变。)
所以我通过 ObjC 代码获取了路径并将其扔给 c++ 函数。
1.将文件放入xcode项目的顶层目录中。并检查它们是否包含在“目标”->“构建阶段”->“复制捆绑资源”中。
2.通过ObjC代码获取路径。
3.并将其扔给c++函数。
4.然后你可以在c++中创建一个绝对文件路径。
这对我有用。
In my case,
- getcwd(NULL, 0) returned "/".
- and couldn't use abusolute path.(It changes on each terminal deployed.)
So I got the path by ObjC code and throw it to c++ function.
1.Put files in top directory of xcode project. And check they are included in "Targets"->"Build Phases"->"Copy Bundle Resources".
2.Get the path by ObjC code.
3.And throw it to c++ function.
4.Then you can make an ablsolute file path in c++.
It worked for me.