Fstream 正在耗尽我的生命
Fstream is draining the life out of me
我的计算机有一个代码编写作业 class,它包括 fstream classes。我必须编写一个管理和存储用户数据的代码。
我在 class 中很难理解这个概念,而 C++ 教科书只想继续向我展示 IPO 图表而不是代码。
如果有人可以帮助我处理我的代码以及如何存储用户输入的数据以便稍后在记事本上查看,我将不胜感激。
预先感谢您提供的所有帮助,我已经搜索了好几天,但无法弄清楚。
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;
struct Baseball {
char FirstName[MAXFIRSTNAME+1];
char LastName[MAXLASTNAME+1];
float AB;
float singles;
float doubles;
float triples;
float HR;
float walks;
double BA;
double SA;
double OBA;
};
int getData(Baseball[]);
void showData(Baseball[], int);
int main()
{
Baseball stats[MAXPLAYERS];
int players = getData(stats);
showData(stats, players);
}
//function: int getData
//description: get the data for all the players the user wishes to input
//input: numerical data for averages and char for name of player
//output: none
int getData(Baseball stats[])
{
int i, players;
cout << "How many aquasocks players would you like to enter data for(1-20): ";
cin >> players;
cout << endl;
for(i=0;i<players;i++) {
cout << "Please enter aquasock #" << i+1 << "'s first name: ";
cin >> stats[i].FirstName;
cout << "Please enter aquasock #" << i+1 << "'s last name: ";
cin >> stats[i].LastName;
cout << "Please enter the number of at bats for player #" << i+1 << ": ";
cin >> stats[i].AB;
cout << "Please enter the number of singles for player #" << i+1 << ": ";
cin >> stats[i].singles;
cout << "Please enter the number of doubles for player #" << i+1 << ": ";
cin >> stats[i].doubles;
cout << "Please enter the number of triples for player #" << i+1 << ": ";
cin >> stats[i].triples;
cout << "Please enter the number of home runs for player #" << i+1 << ": ";
cin >> stats[i].HR;
cout << "Please enter the number of walks for player #" << i+1 << ": ";
cin >> stats[i].walks;
}
double sum = 0;
for(i=0; i<players; i++) {
sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);
cout.precision(3);
cout.setf(ios::fixed);
stats[i].BA = sum / (stats[i].AB);
stats[i].SA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
stats[i].OBA = (sum + stats[i].walks) / (stats[i].AB + stats[i].walks);
}
return players;
}
//function: void showData
//description: uses the data from function int getData to make a calculated chart of the players statistics.
//input: none
//output: chart of the users inputed data from function int getData
void showData(Baseball stats[], int players)
{
int i;
cout << endl << endl;
cout << "lets see how this aquasock stats stack up!\n\n";
cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
cout << setw(6) << "OBA" << "\n";
cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "---" << "\n";
for(i=0; i<players; i++)
{
cout << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}
}
您可以调整 showData
函数以使用 std::ofstream
而不是 std::cout
。
bool writeData(Baseball stats[], int players, std::string const& filePath)
{
std::ofstream ofs(filePath);
if(!ofs.is_open())
return false; // failed to open stream
ofs << endl << endl;
ofs << "lets see how this aquasock stats stack up!\n\n";
ofs << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
ofs << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
ofs << setw(6) << "OBA" << "\n";
ofs << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "---" << "\n";
for(int i = 0; i < players; ++i)
{
ofs << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}
return true;
}
或者要重用代码,您可以使用函数模板,或者更好的是,将您的流传递到函数中(std::ostream
是 std::ofstream
和 [=16 的基础 class =] 是 std::ostream
本身)
void writeData(Baseball stats[], int players, std::ostream &os);
并将 cout
s 更改为 os
s。然后您可以使用它写入控制台:
writeData(stats, players, std::cout);
或档案:
std::ofstream ofs(filePath);
if(!ofs.is_open())
{
// error
}
writeData(stats, players, ofs);
希望这能解决你的疑惑。
我的计算机有一个代码编写作业 class,它包括 fstream classes。我必须编写一个管理和存储用户数据的代码。
我在 class 中很难理解这个概念,而 C++ 教科书只想继续向我展示 IPO 图表而不是代码。
如果有人可以帮助我处理我的代码以及如何存储用户输入的数据以便稍后在记事本上查看,我将不胜感激。
预先感谢您提供的所有帮助,我已经搜索了好几天,但无法弄清楚。
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;
struct Baseball {
char FirstName[MAXFIRSTNAME+1];
char LastName[MAXLASTNAME+1];
float AB;
float singles;
float doubles;
float triples;
float HR;
float walks;
double BA;
double SA;
double OBA;
};
int getData(Baseball[]);
void showData(Baseball[], int);
int main()
{
Baseball stats[MAXPLAYERS];
int players = getData(stats);
showData(stats, players);
}
//function: int getData
//description: get the data for all the players the user wishes to input
//input: numerical data for averages and char for name of player
//output: none
int getData(Baseball stats[])
{
int i, players;
cout << "How many aquasocks players would you like to enter data for(1-20): ";
cin >> players;
cout << endl;
for(i=0;i<players;i++) {
cout << "Please enter aquasock #" << i+1 << "'s first name: ";
cin >> stats[i].FirstName;
cout << "Please enter aquasock #" << i+1 << "'s last name: ";
cin >> stats[i].LastName;
cout << "Please enter the number of at bats for player #" << i+1 << ": ";
cin >> stats[i].AB;
cout << "Please enter the number of singles for player #" << i+1 << ": ";
cin >> stats[i].singles;
cout << "Please enter the number of doubles for player #" << i+1 << ": ";
cin >> stats[i].doubles;
cout << "Please enter the number of triples for player #" << i+1 << ": ";
cin >> stats[i].triples;
cout << "Please enter the number of home runs for player #" << i+1 << ": ";
cin >> stats[i].HR;
cout << "Please enter the number of walks for player #" << i+1 << ": ";
cin >> stats[i].walks;
}
double sum = 0;
for(i=0; i<players; i++) {
sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);
cout.precision(3);
cout.setf(ios::fixed);
stats[i].BA = sum / (stats[i].AB);
stats[i].SA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
stats[i].OBA = (sum + stats[i].walks) / (stats[i].AB + stats[i].walks);
}
return players;
}
//function: void showData
//description: uses the data from function int getData to make a calculated chart of the players statistics.
//input: none
//output: chart of the users inputed data from function int getData
void showData(Baseball stats[], int players)
{
int i;
cout << endl << endl;
cout << "lets see how this aquasock stats stack up!\n\n";
cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
cout << setw(6) << "OBA" << "\n";
cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "---" << "\n";
for(i=0; i<players; i++)
{
cout << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}
}
您可以调整 showData
函数以使用 std::ofstream
而不是 std::cout
。
bool writeData(Baseball stats[], int players, std::string const& filePath)
{
std::ofstream ofs(filePath);
if(!ofs.is_open())
return false; // failed to open stream
ofs << endl << endl;
ofs << "lets see how this aquasock stats stack up!\n\n";
ofs << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
ofs << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "BA" << setw(6) << "SA";
ofs << setw(6) << "OBA" << "\n";
ofs << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
ofs << setw(6) << "---" << "\n";
for(int i = 0; i < players; ++i)
{
ofs << stats[i].FirstName
<< setw(15) << stats[i].LastName
<< setw(8) << stats[i].AB
<< setw(8) << stats[i].singles
<< setw(8) << stats[i].doubles
<< setw(8) << stats[i].triples
<< setw(8) << stats[i].HR
<< setw(8) << stats[i].walks
<< setw(8) << stats[i].BA
<< setw(8) << stats[i].SA
<< setw(8) << stats[i].OBA
<< endl;
}
return true;
}
或者要重用代码,您可以使用函数模板,或者更好的是,将您的流传递到函数中(std::ostream
是 std::ofstream
和 [=16 的基础 class =] 是 std::ostream
本身)
void writeData(Baseball stats[], int players, std::ostream &os);
并将 cout
s 更改为 os
s。然后您可以使用它写入控制台:
writeData(stats, players, std::cout);
或档案:
std::ofstream ofs(filePath);
if(!ofs.is_open())
{
// error
}
writeData(stats, players, ofs);
希望这能解决你的疑惑。