C++ 如何在C+;中拆分一行并从中提取值+;?

C++ 如何在C+;中拆分一行并从中提取值+;?,c++,C++,我在编码一个程序的一部分时遇到了问题,该程序将从一个文件中读取一个名称和10个数字。fie称为grades.dat。数据文件的结构为: Number One 99 99 99 99 99 99 99 99 99 99 John Doe 90 99 98 89 87 90.2 87 99 89.3 91 Clark Bar 67 77 65 65.5 66 72 78 62 61 66 Scooby Doo 78 80 77 78 73 74 75 75 76.2 69 这就是函数获取数据的全部

我在编码一个程序的一部分时遇到了问题,该程序将从一个文件中读取一个名称和10个数字。fie称为grades.dat。数据文件的结构为:

Number One
99 99 99 99 99 99 99 99 99 99
John Doe
90 99 98 89 87 90.2 87 99 89.3 91
Clark Bar
67 77 65 65.5 66 72 78 62 61 66
Scooby Doo
78 80 77 78 73 74 75 75 76.2 69
这就是函数获取数据的全部功能,我甚至不确定这是否正确

void input (float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname)
{
  ifstream infile;

  infile.open ("grades.dat");
  if (infile.fail())
    {
      cout << "Could not open file, please make sure it is named correctly (grades.dat)" << "\n" << "and that it is in the correct spot. (The same directory as this program." << "\n";
      exit(0);
    }
  getline (infile, studentname);
  return;
}
void输入(float&test1、float&test2、float&test3、float&test4、float&test5、float&test6、float&test7、float&test8、float&test9、float&test10、string&studentname)
{
河流充填;
填充开放式(“等级数据”);
if(infle.fail())
{

CUT< P>使用标准C++习语,一次读取两行(或如果不可能失败):


流迭代器在上面的
循环中执行与内部
相同的操作,即它在
double
类型的标记上进行迭代。您可能希望将整个向量插入一个大容器中,该容器包含名称和等级的对
std::pair

90.2
不是一个整数。您的意思是它应该是一个整数吗名称后接十个数字?如果不是,您的程序将如何处理格式错误的输入?是的,这是我的错。编辑问题。谢谢您或
std::vector v((std::istream\u iterator(iss)),std::istream\u iterator());
而不是内部while循环。@Sam-从左到右执行
&&
运算符。因此它先读取
名称
,然后再读取(仅当读取
名称
成功时)它的内容是:
grade\u line
@Rob:好主意;如果在向量中存储确实是目标,那么我完全支持流迭代器。让我编辑。这不起作用。我只是制作了一个快速文件,粘贴了它,添加了缺少的所需组件,并将输入文件更改为我使用的文件,它所做的只是输出第一行数字。我需要它来输出所有的lines@Sam:对我来说很好。您的输入文件与您的问题完全一样吗?缺少哪些“组件”?这编译起来很好,如所示。。。
#include <fstream>
#include <sstream>
#include <string>

#include <iterator>  // only for note #1
#include <vector>    //     -- || --

int main()
{
    std::ifstream infile("thefile.txt");
    std::string name, grade_line;

    while (std::getline(infile, name) && std::getline(infile, grade_line))
    {
        std::istringstream iss(grade_line);

        // See #1; otherwise:

        double d;

        while (iss >> d)
        {
            // process grade
        }
    }
}
std::vector<double> grades (std::istream_iterator<double>(iss),
                            std::istream_iterator<double>());