|
在读取一组数据后, 用两种不同的方式将结果输出到同一个文本文件内. 每种方式有自己的函数来实现. 但我忘记应该如何实现了, 所以每次函数2输出的数据总会将函数1的数据给覆盖掉, 所以我不得不将他们输出到不同的文件了. 下面是我的代码, 请大家指点下.
[code:1]void SortedStuRecords::PrintByGrades() const
{
// Postcondition:
// Print out student records by the letter grades, with the records
// of 'A' first, 'B' the second, ..., 'F' the last.
// Write your implementation here.
ofstream outfile("gfile1.txt");
if(!outfile)
cerr<<"cannot open outfile!\n";
else
{
for(char gradeTemp='A';gradeTemp<='E';gradeTemp++)
{
for(int i=1;i<=length;i++)
if(data[i].letGrade==gradeTemp)
{
outfile<<data[i].lstName<<' '<<data[i].fstName<<"\t\t"
<<data[i].ssn<<" ";
switch(data[i].rank)
{
case 1:outfile<<"Freshment";break;
case 2:outfile<<"Sofomore ";break;
case 3:outfile<<"Junior ";break;
case 4:outfile<<"Senior ";break;
}
outfile<<" "<<data[i].letGrade<<endl;
}
outfile<<endl;
}
}
}
void SortedStuRecords::PrintByRanks() const
{
// Postcondition:
// Print out student records by the ranks, with the records
// of 'Freshmen' first, 'sophomore' the second, 'junior' the
// third, and 'senior' the last.
// Write your implementation here.
ofstream outfile("gfile2.txt");
if(!outfile)
cerr<<"cannot open outfile!\n";
else
{
for(int rankTemp=1;rankTemp<=4;rankTemp++)
{
for(int i=1;i<=length;i++)
if(data[i].rank==rankTemp)
{
outfile<<data[i].lstName<<' '<<data[i].fstName<<"\t\t"
<<data[i].ssn<<" ";
switch(data[i].rank)
{
case 1:outfile<<"Freshment";break;
case 2:outfile<<"Sofomore ";break;
case 3:outfile<<"Junior ";break;
case 4:outfile<<"Senior ";break;
}
outfile<<" "<<data[i].letGrade<<endl;
}
outfile<<endl;
}
}
}[/code:1] |
|