c++ 从文本中逐行读取,并按空格对读取的一行进行分割

ifstream tf("tf.obj", std::ios::in);//打开文件
char s[50] = {0};
string v = "";
string x = "";
string y = "";
string z = "";

for (int i = 0; i < nstrands; i++)
{
    for (int j = 0; j < r.strands[i].size(); j++)
    {
        tf.getline(s, sizeof(s));//逐行读取
        stringstream word(s);//采用字符流格式将读取的str进行空格分隔,并放入str word中
        word >> v;
        word >> x;
        word >> y;
        word >> z;
        //string类型转float类型
        float a = atof(x.c_str());
        float b = atof(y.c_str());
        float c = atof(z.c_str());
        r.strands[i][j][0] = a;
        r.strands[i][j][1] = b;
        r.strands[i][j][2] = c;
    }

}

你可能感兴趣的:(c++)