1. 程式人生 > >c++ 從文字中逐行讀取,並按空格對讀取的一行進行分割

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; } }