使用文本文件中的值进行计算并写入另一个文本文件

Calculation with values from a text file and write into another text file

本文关键字:文本 文件 计算 另一个      更新时间:2023-10-16

我想从文本文件中获取一些值,并在 c++ 中将这些值与固定数字相乘。然后我想将每个值的解决方案写入另一个文本文件中。这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
std::ifstream infile("Test_60.txt");
std::ofstream outfile("Output.txt");
int main(int argc, char** argv)
{
    double max, min, d, s, m;
    max = 0;
    min = 1;
    double x, y, z, R, G, B;
    while (infile >> x >> y >> z >> R >> G >> B)
    {
        if (max < x)
        {
            max = x;
        }
        if (min > x)
        {
            min = x;
        }
        d = max - min;
        s = 60 / d;
        m = s*x;
    }
    outfile << m << endl;
    infile.close();
    outfile.close();
    system("pause");
    return 0;
}

最小值和最大值已经起作用,但是当我尝试将其写入文本文件时,我只得到一个x(或m)值。最后,我想要一个文本文件,其中包含 x 和 y 的乘法值以及输入文件中的 z、R、G、B 值。

此致敬意。

你必须

把语句

max = 0;
min = 1;

进入 while 循环,以确保最小/最大复位。