我一直在学习的类(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 文件所在的目录。
要处理文件,您必须只指定文件名,例如in_file.open("inputFile.txt");
不需要路径。
关于c++ - Xcode 新手无法在 C++ 中打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438393/