Xcode 新手无法在 C++ 中打开文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23438393/
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
New to Xcode can't open files in c++?
提问by user3473255
I've been using windows in a class I've been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn't working any idea why? thanks!
我一直在我参加的课程中使用 windows,但我正在尝试运行一个基本代码来弄清楚如何从 Xcode 上的文件打开/关闭/输入/输出,而我通常在 Visual Studios 上使用的代码是“不知道为什么?谢谢!
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fout.open("outputFile.txt");
fout << input;
}
回答by nepete
Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).
将您的 .txt 文件放在 main.cpp 文件所在的同一目录中(或您喜欢的任何位置)。
In Xcode go to Product > Scheme > Edit Scheme > Run test (on the right) > Options (middle top)
在 Xcode 中,转到 Product > Scheme > Edit Scheme > Run test(在右侧)> Options(中上)
Down under Options check “Use custom working directory” and set it to the directory where you .txt files are located.
在选项下选中“使用自定义工作目录”并将其设置为 .txt 文件所在的目录。
To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.
要处理文件,您必须仅指定文件名,例如 in_file.open("inputFile.txt"); 不需要路径。
回答by SSteve
Here's a completely different approach: Have Xcode copy the input file for you.
这是一种完全不同的方法:让 Xcode 为您复制输入文件。
- Select your project in Xcode
- Select Build Phases
- Click the '+' button to create a new Build Phase
- 在 Xcode 中选择您的项目
- 选择构建阶段
- 单击“+”按钮创建一个新的构建阶段
- Select New Copy Files Build Phase
- 选择新的复制文件构建阶段
- Select Products Directory
- Click the '+' button to add your file
- 选择产品目录
- 单击“+”按钮添加您的文件
- Click Add Other
- 点击添加其他
- Select your input file and click Open
- 选择您的输入文件,然后单击打开
- Check the Copy items…checkbox and click Finish
- 选中复制项目...复选框,然后单击完成
Now every time you build your project, the input file will be copied to the same folder as the executable no matter where it is built. Of course, to see the output file, you'll still need to find the executable in Finder.
现在,每次构建项目时,无论在何处构建,输入文件都将复制到与可执行文件相同的文件夹中。当然,要查看输出文件,您仍然需要在 Finder 中找到可执行文件。
回答by bames53
The answers don't really explain the problem so I thought I'd do that.
答案并不能真正解释问题,所以我想我会这样做。
When you pass a relative path like "inputFile.txt" to file APIs, it's treated as relative to the working directorywhen the program is executed. This is the same as the 'working directory' when you use cmd.exe or Terminal.app or command lines in general. The Unix command pwd
("print working directory") displays the current working directory. On Windows running the command cd
with no arguments performs the same function. (On Unix running cd
with no arguments will change the working directory to the user's home directory.)
当您将诸如“inputFile.txt”之类的相对路径传递给文件 API 时,它会在程序执行时被视为相对于工作目录。当您使用 cmd.exe 或 Terminal.app 或一般命令行时,这与“工作目录”相同。Unix 命令pwd
(“打印工作目录”)显示当前工作目录。在 Windows 上运行cd
不带参数的命令执行相同的功能。(在cd
没有参数的Unix 上运行会将工作目录更改为用户的主目录。)
When you run a program from the command line, the command line shell sets the program's working directory. When you run a program from within an IDE, the IDE sets the working directory. Since, unlike on a command line, there's no obvious answer for what the IDE should set as the working directory, Visual Studio and Xcode set the working directory to different locations by default: Visual Studio sets the working directory to $(ProjectDir)
, the directory containing the Visual Studio project file; Xcode sets the working directory to the build products directory, i.e. the location the executable was written to.
当您从命令行运行程序时,命令行 shell 会设置程序的工作目录。当您从 IDE 中运行程序时,IDE 会设置工作目录。由于与命令行不同,IDE 应该设置什么工作目录没有明显的答案,因此 Visual Studio 和 Xcode 默认将工作目录设置为不同的位置:Visual Studio 将工作目录设置为$(ProjectDir)
,该目录包含 Visual工作室项目文件;Xcode 将工作目录设置为构建产品目录,即可执行文件写入的位置。
Some possible solutions to your problem are:
您的问题的一些可能解决方案是:
Do not use a relative path, and therefore don't depend on the working directory. This isn't much help in making the program more portable, because the absolute paths will also differ between platforms, and so you will still have to 'configure' the program for each platform. In fact using an absolute path is worse, because it means your source code must differ, whereas it would be better to keep that difference confined to each platform's build configuration.
Configure the IDE to use your desired working directory. Visual Studio can be configured by right clicking the project, selecting Configuration Properties > Debugging > Working Directory, and setting the working directory to the desired path (potentially using Visual Studio build variables).
nepete's answerdescribes how to configure the working directly set by Xcode.
Configure the IDE's build process to copy your data files to an appropriate location. In Visual Studio you would do this in a C++ project by configuring the project's Properties > Configuration Properties > Build Events.
SSteve's answercovers how to configure additional build steps in Xcode.
不要使用相对路径,因此不依赖于工作目录。这对于使程序更具可移植性没有多大帮助,因为绝对路径也会因平台而异,因此您仍然需要为每个平台“配置”程序。事实上,使用绝对路径更糟糕,因为这意味着您的源代码必须不同,而最好将这种差异限制在每个平台的构建配置中。
配置 IDE 以使用所需的工作目录。可以通过右键单击项目,选择配置属性 > 调试 > 工作目录,并将工作目录设置为所需路径(可能使用 Visual Studio 构建变量)来配置 Visual Studio。
nepete 的回答描述了如何配置 Xcode 直接设置的工作。
配置 IDE 的构建过程以将数据文件复制到适当的位置。在 Visual Studio 中,您可以通过配置项目的属性 > 配置属性 > 生成事件在 C++ 项目中执行此操作。
SSteve 的回答涵盖了如何在 Xcode 中配置额外的构建步骤。
回答by jendrek
As suggested by nepete, edit the scheme, but use $PROJECT_DIR as the custom working directory. Helps with moving the project around, or working in two different environments (e.g., home and office).
按照 nepete 的建议,编辑方案,但使用 $PROJECT_DIR 作为自定义工作目录。帮助移动项目,或在两种不同的环境(例如,家庭和办公室)中工作。
BTW. $PROJECT_DIR is one of the Xcode Environment Variables, and also helps with passing file names as command line arguments to programs (settable under "Arguments" in the scheme).
顺便提一句。$PROJECT_DIR 是 Xcode 环境变量之一,还有助于将文件名作为命令行参数传递给程序(可在方案中的“参数”下设置)。
回答by SSteve
I'm guessing you have inputFile.txt in the folder that contains your source code. That's not going to work. You need to put it in the folder that contains the generated executable. To find that folder, right-click on your app under Productsand select Show In Finder.
我猜您在包含源代码的文件夹中有 inputFile.txt。那是行不通的。您需要将它放在包含生成的可执行文件的文件夹中。要找到该文件夹,请右键单击“产品”下的应用程序,然后选择“在 Finder 中显示”。
This image shows what it looks like for a command line program. It also shows the Finder window that was opened. As you can see, it is a different folder than the one containing the source code.
此图显示了命令行程序的外观。它还显示打开的 Finder 窗口。如您所见,它与包含源代码的文件夹不同。
回答by joliejuly
I've struggled with the same problem today. I wanted to add C code to my Swift project and my file pointer was always NULL.
我今天一直在努力解决同样的问题。我想将 C 代码添加到我的 Swift 项目中,而我的文件指针始终为 NULL。
Unfortunately, in XCode 9 for iOS app, I couldn't change the working directory. Changing Build phases didn't help me either. After 4+ hours of trial and error, that's what I've come up with finally and it works:
不幸的是,在 iOS 应用程序的 XCode 9 中,我无法更改工作目录。更改构建阶段也没有帮助我。经过 4 个多小时的反复试验,这就是我最终想出的,并且有效:
- when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":
- 将文件复制到 XCode 时,我选择了“创建组”,但我需要选择“创建文件夹引用”:
I created a new objective-c file (.m) and copied all my C code there.
I left untouched .h files (XCode generated bridging header and my own .h file with public functions declaration). Now my project structure looked like this:
我创建了一个新的 Objective-c 文件 (.m) 并将我所有的 C 代码复制到那里。
我没有改动 .h 文件(XCode 生成的桥接头和我自己的带有公共函数声明的 .h 文件)。现在我的项目结构是这样的:
In my dict.m file in place of previous plain c
fopen
part:FILE *dic = fopen("dictionary.txt", "r");
I added obj-C code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dictionary" ofType:@"txt"]; FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
在我的 dict.m 文件中代替之前的普通 c
fopen
部分:FILE *dic = fopen("dictionary.txt", "r");
我添加了 obj-C 代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dictionary" ofType:@"txt"]; FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
And it works now without any problem! It's just amazing!
它现在可以正常工作了!真是太棒了!
ps I decided to write this answer in case it will help someone like me and will save them some time. If you know how to change working directory in XCode 9 for iOS, please, leave me a comment - now I am really curious why I can't find it.
ps我决定写这个答案,以防它对像我这样的人有所帮助并节省他们一些时间。如果您知道如何在 iOS 的 XCode 9 中更改工作目录,请给我留言 - 现在我真的很好奇为什么我找不到它。