c++ - Xcode 新手无法在 C++ 中打开文件?

标签 c++ xcode

我一直在学习的类(class)中使用 windows,但我正在尝试运行一个基本代码来弄清楚如何从 Xcode 上的文件中打开/关闭/输入/输出以及我通常在视觉上使用的代码工作室不知道为什么?谢谢!

#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;


}

最佳答案

将您的 .txt 文件放在您的 main.cpp 文件所在的同一目录中(或您喜欢的任何位置)。

在 Xcode 中,转到 Product > Scheme > Edit Scheme > Run(左侧)> Options(中间顶部)

在“工作目录”选项下选中“使用自定义工作目录”并将其设置为 .txt 文件所在的目录。

enter image description here

要处理文件,您必须只指定文件名,例如in_file.open("inputFile.txt"); 不需要路径。

关于c++ - Xcode 新手无法在 C++ 中打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438393/

相关文章:

c++ - CMake 无法找到 mysqlclient 库

ios - Xcode - RSS 阅读器

c++ - 3D空间中三角形的反射光线

c++ - 用于 C++ 类型转换的 Visual Studio 编译器优化

ios - Xcode 不会导入 appstore.mobileprovision 文件

xcode - 如何在 Xcode 中定位代码中的错误而不是内存地址

iphone - 构建的应用程序成功,但无法在我的 iPhone 设备上启动

iphone - iOS:MFMailComposeViewController 不适用于模型

C++ 类成员未初始化(在构造函数中,但在初始化方法中)

c++ - 链接器错误 undefined reference to class::class (Person::Person in my case)