当我创建以下功能时,

int readDataFromFile(ifstream&  openFileStream, Snowman data[ ]){
    double height;
    double weight;
    double temp;
    bool hat;
    string scarf;
    int count = 0;
    while(openFileStream >> height) {
        openFileStream >> weight;
        openFileStream >> temp;
        openFileStream >> hat;
        openFileStream >> scarf;
    }
    return count;
}

这些部分是错误的;
openFileStream >> height;
openFileStream >> weight;
openFileStream >> temp;
openFileStream >> hat;
openFileStream >> scarf;

结果是:



我将在ifstream函数中打开main(),但是如何将其传递给另一个函数?

最佳答案

看看下面的我的代码段,该代码段可在Windows上使用。注意ans.txt仅包含2个double值

#include <fstream>
#include <iostream>

void printcontents(std::ifstream& inp)
    {
        double val1, val2;

        while (inp)
        {
            inp >> val1;
            inp >> val2;
        }

        std::cout << val1 << " " << val2 << std::endl;
    }

    int main()
    {


            std::ifstream inp("C:\\somefolder\\ans.txt");

            printcontents(inp);

07-27 15:56