C++ 如何将代码结果(在void函数中)写入C+中的文本文件+;

C++ 如何将代码结果(在void函数中)写入C+中的文本文件+;,c++,C++,我正在尝试创建一个程序,加载一个包含20个数字的“grades.txt”文件,计算标准偏差,并在屏幕上以及标题为“results.txt”的单独文本文件中输出结果 程序运行良好,并输出我想要的结果。唯一的问题是,我似乎不知道如何将计算结果(即总和、平均值、标准偏差)输出到文本文件中 代码中的输出文本文件正在成功地输出我发送给它的“结果”消息 我只是不知道如何获取“void filereadernc(ifstream&file)”函数中的结果以及“void filewriter(ofstream&

我正在尝试创建一个程序,加载一个包含20个数字的“grades.txt”文件,计算标准偏差,并在屏幕上以及标题为“results.txt”的单独文本文件中输出结果

程序运行良好,并输出我想要的结果。唯一的问题是,我似乎不知道如何将计算结果(即总和、平均值、标准偏差)输出到文本文件中

代码中的输出文本文件正在成功地输出我发送给它的“结果”消息

我只是不知道如何获取“void filereadernc(ifstream&file)”函数中的结果以及“void filewriter(ofstream&file)”函数的文本文件中的输出

#包括
#包括
#包括
//调用std名称空间
使用名称空间std;
void fileopenner(ifstream&file);
无效文件读取器(ifstream和file);
作废文件关闭器(ifstream&file);
void filereadernc(ifstream&file);
作废文件创建(流和文件);
作废文件写入程序(流和文件);
作废文件关闭器(流和文件);
int main()
{
//声明
ifecet;
流的结果;
//再次打开onc
文件开放器(ecet);
文件阅读器(ecet);
文件关闭器(ecet);
文件创建(结果);
文件编写器(结果);
文件关闭(结果);
列表[i];
}
void filereadernc(ifstream&file)//文件读取器
{
浮动总和=0.0,平均值=0.0,标准偏差=0.0;
国际清单[100];
int j=0;
文件>>列表[j];
而(!file.eof())
{
j++;
文件>>列表[j];
}

cout您的代码有点复杂,但这里有一个技巧,您可以通过调用filewriter方法并在args中传递您想要在文件中写入的内容来代替每次(cout)在终端中显示结果

按以下方式调用函数:

filewriter(ofstream &file, sum);
void filewriter(ofstream &file, float result)
{

file << results;

}
并通过以下方式实施:

filewriter(ofstream &file, sum);
void filewriter(ofstream &file, float result)
{

file << results;

}
void filewriter(流文件、浮点结果)
{

file您的代码有点复杂,但这里有一个技巧,您可以通过调用方法filewriter并在args中传递您想在文件中写入的内容,来代替每次(cout)将结果显示在终端上

按以下方式调用函数:

filewriter(ofstream &file, sum);
void filewriter(ofstream &file, float result)
{

file << results;

}
并通过以下方式实施:

filewriter(ofstream &file, sum);
void filewriter(ofstream &file, float result)
{

file << results;

}
void filewriter(流文件、浮点结果)
{
文件
我只是不知道如何获取“void filereadernc(ifstream&file)”函数中的结果以及“void filewriter(ofstream&file)”函数的文本文件中的输出

我建议将程序划分为三个独立部分:

  • 读取数据
  • 处理数据
  • 写下数据
  • 为了能够做到这一点,您必须首先定义您的数据。使用
    struct

    struct MyData
    {
      // ... fill in the details.
    };
    
    职能:

    void readData(std::istream& in, MyData& data)
    {
       // Read the contents of the stream and flesh out data
    }
    
    void readData(std::string const& file, MyData& data)
    {
       // Read the contents of file and flesh out data
       std::ifstream in(file);
       readData(in, data);
    }
    
    void processData(MyData& data)
    {
       // Do whatever you need to do to process the data.
    }
    
    void writeData(std:::ostream& out, MyData const& data)
    {
       // Write the contents of the data to the stream.
    }
    
    void writeData(std:::string const& file, MyData const& data)
    {
       // Write the contents of the data to the file.
       std::ofstream out(file);
       writeData(out, data);
        }
    
    现在,
    main
    可以:

    int main()
    {
       MyData data;
    
       // read the data.
       readData("C:/Users/Desktop/grades.txt", data);
    
       // Process the data.
       processData(data);
    
       // Write the data out to a file and std::cout
       writeData("C:/Users/Desktop/results.txt", data);
       writeData(std::cout, data);
    }
    
    如果有意义,您也可以将数据分为输入数据和结果数据

    struct InputData { ... };
    struct ResultsData { ... };
    
    然后适当地更新函数

    我只是不知道如何获取“void filereadernc(ifstream&file)”函数中的结果以及“void filewriter(ofstream&file)”函数的文本文件中的输出

    我建议将程序划分为三个独立部分:

  • 读取数据
  • 处理数据
  • 写下数据
  • 为了能够做到这一点,您必须首先定义您的数据。使用
    struct

    struct MyData
    {
      // ... fill in the details.
    };
    
    职能:

    void readData(std::istream& in, MyData& data)
    {
       // Read the contents of the stream and flesh out data
    }
    
    void readData(std::string const& file, MyData& data)
    {
       // Read the contents of file and flesh out data
       std::ifstream in(file);
       readData(in, data);
    }
    
    void processData(MyData& data)
    {
       // Do whatever you need to do to process the data.
    }
    
    void writeData(std:::ostream& out, MyData const& data)
    {
       // Write the contents of the data to the stream.
    }
    
    void writeData(std:::string const& file, MyData const& data)
    {
       // Write the contents of the data to the file.
       std::ofstream out(file);
       writeData(out, data);
        }
    
    现在,
    main
    可以:

    int main()
    {
       MyData data;
    
       // read the data.
       readData("C:/Users/Desktop/grades.txt", data);
    
       // Process the data.
       processData(data);
    
       // Write the data out to a file and std::cout
       writeData("C:/Users/Desktop/results.txt", data);
       writeData(std::cout, data);
    }
    
    如果有意义,您也可以将数据分为输入数据和结果数据

    struct InputData { ... };
    struct ResultsData { ... };
    

    然后适当地更新函数。

    这段代码看起来过于复杂。
    ofstream::open
    ifstream::open
    都是您所需要的。当在
    ofstream
    上使用时,将创建文件。这足够简单,它可能都存在于
    main
    中,而不需要额外的一行函数只使用过一次的。此代码看起来过于复杂。
    ofstream::open
    ifstream::open
    是您所需要的全部。当在
    ofstream
    上使用时,将创建文件。这足够简单,因此它可能都存在于
    main
    中,而无需使用额外的单行函数一次。与单输出线相比有什么优势?看起来您增加了函数调用和返回的开销。请参阅@R Sahu答案以获得最佳解决方案。我刚刚编写了一个与您的实现相匹配的解决方案,它非常难看。我将尝试使用@R Sahu的答案继续进行此操作,但也将尝试您的解决方案和see如果它有效。谢谢!欢迎您,如果有任何问题,请在这里发表评论,我很乐意提供帮助。仍然无法理解如何用我上面发布的代码实现您所说的。我将继续尝试,希望输出txt文件有效,并显示我从filereadernc void函数获得的结果广告是什么vantage与单输出线的对比?看起来您增加了函数调用和返回的开销。请参阅@R Sahu answer以获得最佳解决方案。我刚刚编写了一个与您的实现相匹配的解决方案,这非常难看。我将尝试使用@R Sahu的答案继续进行此操作,但也将尝试您的解决方案,看看它是否有效。Thank你!不客气,如果有任何问题,请在这里发表评论,我很乐意提供帮助。我仍然不知道如何用上面发布的代码实现你所说的。我会继续尝试,希望输出的txt文件能够工作,并显示我从filereadernc void函数获得的结果