3

我写了一个简单的程序(C++),它接受 20 个数字并将它们按升序排序。现在我想将程序中的动作保存在像“num.txt”这样的文件中。你能解释一下我应该做什么改变吗?

#include <iostream>
#include <iomanip>
#include <conio.h> 
using namespace std;   

int main() {   

    int x[21], s;
    int j,i;

    for (int i = 1; i < 21; i++)
    {
        cout << setw(11) << i << ": ";
        cin >> x[i];
    }
    for (int i = 1; i < 21; i++)
    {
        for (int j = i+1; j < 21; j++)
        {
            if (x[j] < x[i])
            {
                s = x[j];
                x[j] = x[i];
                x[i] = s;
            }
        }
    }

    cout << endl;
    for (int i = 1; i < 21; i++)
    {
        cout << i << ": ";
        cout << x[i] << "\t";
        if (i % 5 == 0)
        {
            cout << endl;
        }
    }
    getch();
    return 0; 
}

我知道这很简单,但我几天前才开始,我是新手。

4

5 回答 5

5

要将数字输出到文件,您可以将std::ofstream其用于输出流并替换cout为您用于流的变量名称。

std::ofstream outfile;
outfile.open("num.txt");
for (int i = 1; i < 21; i++)
{
    outfile << i << ": ";
    outfile << x[i] << "\t";
    if (i % 5 == 0)
    {
        outfile << std::endl;
    }
}
outfile.close();

您还可以更进一步,添加输入验证并使用标准库中的组件来处理您要完成的大部分工作。例如,我建议std::vector不要将数字存储在数组中。您还可以使用std::sort对数据进行排序,而不是自己实现。

#include <vector>       // vector
#include <fstream>      // fstream
#include <algorithm>    // sort
#include <iostream>

int main()
{
    std::vector<int>    numbers;

    while(numbers.size() != 20)
    {
        int value;

        if(!(std::cin >> value))
        {
            std::cout << "you must enter a number" << std::endl;
        }
        else
        {
            numbers.push_back(value);
        }
    }

    // Do the sort. Pretty easy huh!
    std::sort(numbers.begin(), numbers.end());

    std::ofstream outfile;
    outfile.open("num.txt");
    if(outfile.is_open() == false)
    {
        std::cout << "Unable to open num.txt" << std::endl;
    }
    else
    {
        for(size_t i = 0; i < numbers.size(); i++)
        {
            outfile << i << ": ";
            outfile << numbers[i] << "\t";
            if (i % 5 == 0)
            {
                outfile << std::endl;
            }
        }
        outfile.close();
    }
}
于 2013-06-04T15:29:11.240 回答
4

在您的代码中使用它

#include <fstream>

ofstream outputFile;
outputFile.open("outputfile.txt");
outputFile << value << endl;
outputFile.close();
于 2013-06-04T15:01:01.397 回答
3

一种解决方案是使用ofstream (在“fstream.h”中,与std::cout非常相似):

ofstream out("num.txt");
if (out.is_open() == false)
{
    cout << "Error! Couldn't open file!" << endl;
}
else
{
    out << "\n";
    for (int i = 1; i < 21; i++)
    {
        out << i << ": ";
        out << x[i] << "\t";
        if (i % 5 == 0)
        {
            out << "\n";
        }
    }
    out.close();
}
于 2013-06-04T15:19:55.160 回答
2

这是您的所有更改的代码,现在您只需要复制粘贴(我已经在我添加的行上发表了评论)

#include <iostream>
#include <iomanip>
#include <conio.h> 
#include <fstream>//file stream 

using namespace std;   

int main() {   

    int x[21], s;
    int j,i;

    for (int i = 1; i < 21; i++)
    {
        cout << setw(11) << i << ": ";
        cin >> x[i];
    }
    for (int i = 1; i < 21; i++)
    {
        for (int j = i+1; j < 21; j++)
        {
            if (x[j] < x[i])
            {
                s = x[j];
                x[j] = x[i];
                x[i] = s;
            }
        }
    }

    ofstream out; //output file stream
    out.open("num.txt"); //opening (and creating output file named out


    //cout << endl;
    for (int i = 1; i < 21; i++)
    {
        //cout << i << ": ";
        out << i << ": "; //printing in output file
      //  cout << x[i] << "\t";
        out << x[i] << "\t"; //printing in output file
        if (i % 5 == 0)
        {
            //cout << endl;
            out << endl; //printing in output file
        }
    }
    //getch();
    out.close(); //closing output file
    return 0; 
}
于 2013-06-04T16:28:13.943 回答
2

如果您从命令行运行程序,请键入以下内容(运行时)以将输出转发到文件:

myApp > num.txt

您也可以使用<开关来指定要从中获取输入的文件

您可以在这里找到更多信息。

于 2013-06-04T14:58:46.367 回答