#include<iostream>
#include<Windows.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
char name[32];
int age;
ifstream infile;
infile.open("user2.txt");
string line;
while (1)
{
getline(infile, line);
if (infile.eof())
{
break;
}
sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name,sizeof(name), &age);
cout << "姓名:" << name << "年龄:" << age<<endl;
}
infile.close();
system("pause");
return 0;
}
注意: "姓名:%s 年龄:%d"这里的冒号要与写入时的冒号相一致(圆角或半角)。
这里是按C语言的读法,所以这里的name要用char型