从文件读取到字符数组

时间:2016-10-31 17:27:05

标签: c++ arrays file sorting

我试图从文件中读取一些字符,在数组中对它们进行排序并将它们打印到新文件中。我遇到的问题是什么都没发生。我已经试图弄清楚了几天,我根本无法想出一个解决方案或找到一个解决方案,因为这是它在各处做的,但由于某种原因我不能让它工作。

int main() {
    int i = 0, sortType;
    char c[100];
    cout << "Input the type of sorting you want...\nBubble sort: Press 1 \nSelection sort: Press 2 \nInsertion sort: Press 3 \nQuick sort: Press 4\n\n";
    cin >> sortType;


    ifstream inFile("AllAlpha.txt");
    if (!inFile) {
        cout << "\nFile is unavailible";
        return 0;
    }

    for (;; i++) {
        if (!inFile.eof())
            break;
        inFile.get(c[i]);
    }

    switch (sortType) {
    case 1: bubbleSort(c, i);
    case 2: selectionSort(c, i);
    case 3: insertionSort(c, i);
    case 4: quickSort(c, 0, i - 1);
    }

    ofstream outFile("output.txt");
    if (!outFile) {
        cout << "\nFile is unavailible";
        return 0;
    }


    for (int j = 0; j < i; j++) {
        outFile << c[j] << " ";
    }

    inFile.close();
    outFile.close();
}

0 个答案:

没有答案