如何在 Xcode 中使用参数
How to work with arguments in Xcode
这里的新人。 我正在尝试学习使用Mac OS X C++,并且在让调试器在Eclipse或Netbeans中工作时遇到了严重的问题(由于某种疯狂的原因无法获得gdb),因此决定尝试使用Xcode。 我编写了一个简单的排序程序,但不知道如何填充输出文件。 这是我到目前为止所做的:
- 创建了一个名称列表,并将其作为"名称.txt"保存在"排序"文件夹中。
- 进入 Xcode 中的"编辑方案"选项卡,并添加了两个参数 Name.txt 和 Output.txt。
- 运行程序没有错误或问题,但输出.txt未创建。
- 在 Xcode 中,我使用"添加文件进行排序".txt拉入 Names,并创建了一个空白的 Output.txt 文件并将其保存在 Sort 文件夹中。 然后我也将 Output.txt 拉入 Xcode。
- 运行了程序,但仍然有一个空白的输出.txt文件。
这是编写的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Constants
#define BUFFER_SIZE 50
#define ARRAY_SIZE 20
// Global variables
int numElements = 0;
//function prototypes
void sort(string elements[]); // sort an array of strings in ascending order
void swap(string& s1, string& s2); // swap s1 and s2
int main(int argc, char *argv[])
{
char buffer[BUFFER_SIZE];
string listOfNames[ARRAY_SIZE];
string inputFileName;
string outputFileName;
ifstream inputFile;
ofstream outputFile;
if(argc != 3) {
cout << "Error: Please enter input and output files ";
cout << "as command line arguments !" << endl;
}
else {
inputFileName = argv[1];
outputFileName = argv[2];
inputFile.open(inputFileName.c_str());
outputFile.open(outputFileName.c_str());
// Read names from input file and store into array
while(!inputFile.eof() && numElements < (ARRAY_SIZE - 1)) {
inputFile.getline(buffer, BUFFER_SIZE);
string p = string(buffer);
listOfNames[numElements] = p;
numElements++;
}
// Sort elements in array
sort(listOfNames);
// Print elements in array to output file
for(int i = 0; i < numElements; i++) {
outputFile << listOfNames[i] << endl;
}
inputFile.close();
outputFile.close();
}
cout << "Sorting done!!!" << endl;
return 0;
}// end main
// perform bubble sort
// sort names in ascending order
void sort(string elements[]) {
bool change = true;
while(change) {
change = false;
for (int i = 0; i < (numElements - 1); i++) {
if (elements[i] > elements[i + 1]) {
swap(elements[i], elements[i+1]);
change = true;
}
}
}
}
// swapping 2 string
void swap(string& s1, string& s2) {
string temp = s1;
s1 = s2;
s2 = temp;
}
我相信代码是正确的,因为它在 Eclipse 中工作......我只是不知道如何让 Xcode 生成输出文件。
问题几乎可以肯定是你没有在程序的工作目录中查找。这是创建"输出.txt"的地方,默认情况下,IIRC位于Xcode在~/Library文件夹内的Xcode应用程序支持区域中为项目创建的目录中。
我相信您可以在方案中设置工作目录。您还可以使用管理器打开 Xcode 为项目创建的文件夹。
请注意,这会影响您的输出.txt和您的姓名.txt; std::ifstream
不会仅仅因为您已将文件添加到 Xcode 项目而打开该文件。您需要使用绝对路径或将文件放在工作目录中。
它应该在与排序相同的文件夹中构建.cpp
不。它将在程序运行的"当前工作目录"中创建。不同的 IDE 会将其设置为不同的位置。如果您从 Terminal.app 运行程序,那么它将是您运行程序时所在的任何位置。
我在计算机上找不到任何输出.txt
由于默认位置在~/Library目录中的某个地方,因此只有在选中"搜索系统文件夹"选项或任何名称时,Finder中的目录搜索才会找到该文件。最好只查找 Xcode 在您的方案中或 Xcode 的组织者中设置当前工作目录的内容。
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- GLEW 参数在 Xcode 中完成
- 如果标头位于预编译标头 xcode 中,则找不到默认模板参数
- 如何在Xcode 5中使用编辑方案将参数传递给main.cpp
- Xcode 不会将参数传递给可执行文件C++
- C++ [Xcode] 中的参数数量可变
- 在 Xcode [c++] 中在断言 (assert.h) 中定义 lambda 时,为类似函数的宏调用提供过多参数会导
- 如何在 Xcode 中使用参数
- Xcode LLVM 5.0错误-将来不会出现命令行参数硬错误
- 自动完成不显示在xcode中有默认值的函数参数
- XCode 4:文件作为命令参数
- Cocos2dx 215 Xcode 5.1 新项目未构建(叮叮当当:错误:未知参数:'-websockets')