while ( ifsW >> id && ifsW >> name && ifsW >> sex && ifsW >>phone) { Wnum++; }phone读入不进去怎么办
时间: 2023-08-07 12:19:13 浏览: 85
如果phone读入不进去,可能是因为读入的数据格式有误或者数据本身就没有phone这一项。可以先检查一下数据文件中是否有phone这一项,如果有的话,可以尝试使用getline函数来读入phone,例如:
```
while ( ifsW >> id && ifsW >> name && ifsW >> sex )
{
getline(ifsW, phone);
Wnum++;
}
```
这样就可以将phone读入到字符串变量phone中了。如果phone的格式不是字符串而是数字,可以使用cin来读入,例如:
```
while ( ifsW >> id && ifsW >> name && ifsW >> sex )
{
int phone_num;
ifsW >> phone_num;
string phone = to_string(phone_num);
Wnum++;
}
```
这样就可以将phone读入到字符串变量phone中了。