c++ - 代码输出废话到输出文件而不是字符串?

标签 c++ string file filestream stringstream

以下代码适用于我必须执行的项目,我收到一个文本文件,其中包含学生的名字和姓氏,后跟他的成绩。然后我必须将其转换成一个输出文件,其中包含他的名字和他的平均分。我收到的文件中有多个学生逐行分开。输出应该看起来相对像

Rzam, Look           = 0.00
Bambi, Lambi         = 40.47
Coop, Jason          = 27.31

但我的只是打印垃圾,例如

0x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fff b08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.000x7fffb08e8698= 0.00

这是我目前所拥有的:

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct Student
{
    string fname;
    string lname;
    double average;
};

int read(ifstream &fin, Student s[]);

void print(ofstream &fout, Student s[], int amount);


int main()
{
    const int size = 10;
    ifstream fin;
    ofstream fout;
    string inputFile;
    string outputFile;
    Student s[size];

    cout << "Enter input filename: ";
    cin >> inputFile;
    cout << "Enter output filename: ";
    cin >> outputFile;
    cout << endl;

    fin.open(inputFile.c_str());
    fout.open(outputFile.c_str());

    read(fin , s);
    print(fout, s, size);

    fin.close();
    fout.close();

}

int read(ifstream &fin, Student s[])
{
    string line;
    string firstName;
    string lastName;
    double score;
    double total;
    int i=0;
    int totalStudents=0;
    Student stu;

    while(getline(fin, line)){
        istringstream sin;
        sin.str(line);

        while(sin >> firstName >> lastName){
            stu.fname = firstName;
            stu.lname = lastName;

            while(sin >> score){
            total *= score;
            i++;
            }
            stu.average = (total/i);
        }
        s[totalStudents]=stu;
        totalStudents++;
    }
    return totalStudents;
}

void print(ofstream &fout, Student s[], int amount)
{
    ostringstream sout;
    for(int i = 0; i<amount; i++)
    {
        sout << left << setw(20) << s[i].lname << ", " << s[i].fname;
        fout << sout << setprecision(2) << fixed << "= " << s[i].average;
    }
}

最佳答案

您有一些错误,这些错误加起来导致了您的问题:

  1. 在您的print 函数中,您写入ostringstream,然后尝试将其写入文件流。这很好,但它正在打印 ostringstream 缓冲区的地址。因此进行此更改将导致它打印内容:

    fout << sout.str() << setprecision(2) << fixed << "= " << s[i].average;
    

注意.str()的用法。虽然你在这里根本不需要临时流......

  1. 您没有在输出中放置换行符,因此所有内容都以一行结尾,难以阅读:

所以再做一个改变,让它看起来像这样:

fout << sout.str() << setprecision(2) << fixed << "= " << s[i].average << '\n';
  1. 您需要将 ostringstream sout; 放在循环内,因此每次都会重置它。否则你会得到奇怪的复合输出。

  2. 您没有使用通过您的 read 函数计算的学生人数!所以它总是尝试打印 10!做这样的事情:

    int count = read(fin , s);
    print(fout, s, count);
    
  3. 如果没有读取分数,我认为您的分数将被零除。所以你应该加一张支票。

  4. 您应该确保阅读的学生不超过 size 个。或者更好的是,只需将它们放在 std::vector 中并从函数中返回它。它更简单且不易出错。

  5. 每次开始阅读一个学生时,您需要重新设置i,否则后面的学生会分得太远。每个都需要有一个独立的计数。

我不知道这些是否是唯一的问题,但它肯定会让您走上正确的轨道:-)

关于c++ - 代码输出废话到输出文件而不是字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353633/

相关文章:

jquery - 使用jquery获取上传文件名到文本框中

string - Groovy 字符串连接

ruby - 为什么 split (' ' ) 试图变得(太)聪明?

java - 如何在Java中更改文件夹的名称

c++ - 有什么理由不对 C++ 基于范围的 for 循环使用 auto& 吗?

c# - 您如何检查字符串列表是否与您的短语中的所有单词匹配?

c - 在 C 中执行文件操作时,数字被 append 而不是被添加到 int 变量中

c++ - 在不可编辑的组合框中显示的默认项目

c++ - 如何重新分配 boost shared_ptr

javascript - 如何通过 ms chakracore JavaScript 引擎调用 C++ 函数或方法?