C ++:存储文本文件

时间:2013-06-04 14:56:20

标签: c++

我写了一个简单的程序(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; 
}

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

5 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:4)

在您的代码

中使用此功能
#include <fstream>

ofstream outputFile;
outputFile.open("outputfile.txt");
outputFile << value << endl;
outputFile.close();

答案 2 :(得分: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();
}

答案 3 :(得分:2)

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

myApp > num.txt

你也可以使用<开关指定一个文件来输入

You can find more information here.

答案 4 :(得分: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; 
}