gpt4 book ai didi

c++ - 如何使用ifstream作为函数参数?

转载 作者:行者123 更新时间:2023-12-02 11:06:50 26 4
gpt4 key购买 nike

当我创建以下功能时,

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;

结果是:

Invalid operands to binary expression ('std::__1::ifstream' (aka 'basic_ifstream') and 'double')



我将在 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);

关于c++ - 如何使用ifstream作为函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com