如何在 Xcode 中使用参数

How to work with arguments in Xcode

本文关键字:参数 Xcode      更新时间:2023-10-16

这里的新人。 我正在尝试学习使用Mac OS X C++,并且在让调试器在Eclipse或Netbeans中工作时遇到了严重的问题(由于某种疯狂的原因无法获得gdb),因此决定尝试使用Xcode。 我编写了一个简单的排序程序,但不知道如何填充输出文件。 这是我到目前为止所做的:

  1. 创建了一个名称列表,并将其作为"名称.txt"保存在"排序"文件夹中。
  2. 进入 Xcode 中的"编辑方案"选项卡,并添加了两个参数 Name.txt 和 Output.txt。
  3. 运行程序没有错误或问题,但输出.txt未创建。
  4. 在 Xcode 中,我使用"添加文件进行排序".txt拉入 Names,并创建了一个空白的 Output.txt 文件并将其保存在 Sort 文件夹中。 然后我也将 Output.txt 拉入 Xcode。
  5. 运行了程序,但仍然有一个空白的输出.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 的组织者中设置当前工作目录的内容。