在 Xcode 上用 C++ 创建文件

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

File creation in C++ on Xcode

c++xcodemacosfile

提问by William Oliver

This is supposed to create a file in my project directory called "tuna.txt". When I run it, it compiles successfully, however no file is created. I am on a mac using xcode. I have searched my computer for other places where it might have been created, but it seems as if the file was not created at all. Any ideas as to why it doesn't work?

这应该在我的项目目录中创建一个名为“tuna.txt”的文件。当我运行它时,它编译成功,但是没有创建文件。我在使用 xcode 的 Mac 上。我已经在我的计算机上搜索了可能已创建它的其他地方,但似乎根本没有创建该文件。关于为什么它不起作用的任何想法?

#include <iostream>
#include <fstream>
using namespace std;
int main(void){
    ofstream file;
    file.open("tuna.txt");

    file << "I love tuna and tuna loves me!\n";
    file.close();
    return 0;
}

回答by WhozCraig

I assure you that barring errors (which you're not checking for) a file is created. Xcode has a tendency to use the final build-dir as the current working directory when running from the IDE. you can change this by editing the active Scheme.

我向你保证,除非错误(你没有检查),否则会创建一个文件。从 IDE 运行时,Xcode 倾向于使用最终构建目录作为当前工作目录。您可以通过编辑活动方案来更改此设置。

  1. Click on the Project box to the right of the STOP button on the main toolbar
  2. Select Edit Scheme
  3. Select the "Run" sub scheme in the left pane list.
  4. Select the Options tab,
  5. Check the "Use Custom Working Directory" checkbox
  6. Set the working directory to some place you know(like your project root folder).
  1. 单击主工具栏上 STOP 按钮右侧的 Project 框
  2. 选择编辑方案
  3. 在左窗格列表中选择“运行”子方案。
  4. 选择选项选项卡,
  5. 选中“使用自定义工作目录”复选框
  6. 将工作目录设置到您知道的某个位置(例如您的项目根文件夹)。

Note: This is also where you will setup any command line arguments (those are on the Arguments tab, not the Options tab), should you desire to do so.

注意:这也是您可以设置任何命令行参数的地方(它们位于参数选项卡上,而不是选项选项卡上),如果您愿意的话。

回答by Alfred

First of all you must check whether file has been opened/createdor not. Then you should search for the file. Most probably the file hasn't been created yet. Here is the code:

首先,您必须检查文件是否已opened/created存在。然后您应该搜索该文件。很可能该文件尚未创建。这是代码:

#include <iostream>
#include <fstream>
using namespace std;

int main(void){

ofstream file;
file.open("tuna.txt");
if(file.is_open())
{
   file << "I love tuna and tuna loves me!\n";
    file.close();
}
else
   cout<< "No file has been created!\n";

  return 0;
}

As you haven't given an absolute path to open function.See the folder where your code file is. Most probably the file will be there.

由于您没有给出打开函数的绝对路径。请参阅代码文件所在的文件夹。该文件很可能会在那里。

回答by Kevin Mitchell

In the Productsfolder (in the Project Navigatorof the Navigatortab on the left-hand side of the Xcode IDE) you will find the executable. Click on the executable.

Products文件夹中(在Xcode IDE 左侧导航器选项卡的项目导航器中),您将找到可执行文件。单击可执行文件。

If not already shown, make sure the Utilitiestab on the right hand-side of the Xcode IDE is shown and the Show the file inspectoris selected.

如果尚未显示,请确保显示了 Xcode IDE 右侧的Utilities选项卡并选择了Show the file inspector

From the inspector, you will see Full Pathshowing the path to the executable, and at the end of it, there will be an arrow. Clicking on this arrow will open up the Finderwindow to that location, and this is where you should also see all the text files and other files that have been created from within the program.

检查器中,您将看到显示可执行文件路径的完整路径,并且在它的末尾会有一个箭头。单击此箭头将打开该位置的Finder窗口,您还应该在此处看到从程序中创建的所有文本文件和其他文件。

PS. The reason that you couldn't find the tuna.txt file when using the search is because it is in a hidden folder along with the executable.

附注。使用搜索时找不到 tuna.txt 文件的原因是它与可执行文件一起位于隐藏文件夹中。