程序只输入一行到文本文件
Program inputs only one line into text file
我的程序只从文本文件中输入一次数字。我还注意到给定数字的最后一行用于计算。如果我试图在 while 循环中这样做是不是很糟糕?如果是这样,我还能怎么做?
人数是:
5
100 1 20
120 1 45
90 1 05
105 1 25
100 1 35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double d,a,b,c,g=0;
ifstream file("Text.txt");
if(file.is_open())
{
file >> d;
cout << d << endl;
while(file >> a >> b >> c)
{
cout << left << setw(3) << a << " " << b << " " << c << endl;
g=a/(b*60+c);
ofstream files("Result.txt");
files << "Speed: " << g << " m/s" << endl;
}
}
return 0;
}
这条线有问题在循环里面
ofstream files("Result.txt");
它会在您每次读取输入时创建一个新 文件。所以它只会包含最后一个输入的输出。
如果将该行移动到 while
之前,您将在同一个文件中获得所有输出。
我的程序只从文本文件中输入一次数字。我还注意到给定数字的最后一行用于计算。如果我试图在 while 循环中这样做是不是很糟糕?如果是这样,我还能怎么做?
人数是:
5
100 1 20
120 1 45
90 1 05
105 1 25
100 1 35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double d,a,b,c,g=0;
ifstream file("Text.txt");
if(file.is_open())
{
file >> d;
cout << d << endl;
while(file >> a >> b >> c)
{
cout << left << setw(3) << a << " " << b << " " << c << endl;
g=a/(b*60+c);
ofstream files("Result.txt");
files << "Speed: " << g << " m/s" << endl;
}
}
return 0;
}
这条线有问题在循环里面
ofstream files("Result.txt");
它会在您每次读取输入时创建一个新 文件。所以它只会包含最后一个输入的输出。
如果将该行移动到 while
之前,您将在同一个文件中获得所有输出。