C++ Read Data from File into Struct

来源:互联网 发布:vs for mac 下载 编辑:程序博客网 时间:2024/05/10 18:08
#include <iostream>#include <string>#include <fstream>using namespace std;#define N 2struct Student{ string name; int age;};int main(){ struct Student stu[N]; ifstream fin("text.txt"); if(!fin){  cout<<"File open error!\n";  return 1; } for(int i = 0; i < N; i++) {   fin >> stu[i].name;  fin >> stu[i].age; } for(i = 0; i < N; i++) { cout<<stu[i].name<<" "<<stu[i].age<<endl; } fin.close(); return 0;}


原创粉丝点击