Xcode 中的 C++ 程序不使用 outFile 输出简单的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16779149/
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-27 20:38:49  来源:igfitidea点击:

C++ Program in Xcode not outputting simple text file using outFile

c++fstreamfile-io

提问by Nearpoint

I am running a program using this simple example code to output a text file. I am using Xcode and simply started a new C++ project from command line tools. For some reason the program does not output any file onto my Mac. Please help figure out why XCode will not output any file to my computer? Thanks!

我正在使用这个简单的示例代码运行一个程序来输出一个文本文件。我正在使用 Xcode,只是从命令行工具启动了一个新的 C++ 项目。出于某种原因,该程序不会将任何文件输出到我的 Mac 上。请帮助找出为什么 XCode 不会向我的计算机输出任何文件?谢谢!

#include <iostream>
#include <fstream>

using namespace std;

int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}

回答by Nearpoint

Dang I can't believe I figured it out, it was an option in Xcode. So I clicked on the bar at the top of xcode near the stop button with the text (Project Name > My Mac 64-bit)

我简直不敢相信我想通了,这是 Xcode 中的一个选项。所以我点击了 xcode 顶部靠近停止按钮的栏,上面有文本(项目名称 > 我的 Mac 64 位)

Then clicked edit scheme. The clicked on the options tab and clicked use custom working directory. Then selected a working directory. Now the text file appears!

然后点击编辑方案。单击选项选项卡并单击使用自定义工作目录。然后选择一个工作目录。现在文本文件出现了!

回答by peterdcasey

As mentioned above, editing the scheme worked for me but getting to the scheme was different.

如上所述,编辑方案对我有用,但进入方案是不同的。

  • Use the ProductMenu
  • Select the Schemeoption
  • Select the Edit Schemeoption
  • Click the OptionsTab
  • Tick the "Working Directory" item
  • Click the small icon at the right end of the text box.
  • Select the directory
  • 使用产品菜单
  • 选择方案选项
  • 选择编辑方案选项
  • 单击选项选项
  • 勾选“工作目录”项
  • 单击文本框右端的小图标。
  • 选择目录

I also had the Run/Debugoption selected in the left hand pane of the window.

我还在窗口的左侧窗格中选择了运行/调试选项。

回答by Lirux

To find the current working directory, in the project navigator, under the "Products" folder, right-click the product and choose "Show in Finder". In this directory you will also find the compiled code.

要查找当前工作目录,请在项目导航器中的“产品”文件夹下,右键单击产品并选择“在 Finder 中显示”。在此目录中,您还将找到已编译的代码。

For example: ~/Library/Developer/Xcode/DerivedData/<project name>-gweghgfqkjkidjfhcgetdryechjz/Build/Products/Debug

例如:~/Library/Developer/Xcode/DerivedData/<项目名称>-gweghgfqkjkidjfhcgetdryechjz/Build/Products/Debug