c++怎么按行读取txt文件,并写到结构体数组中

如题所述

首席需要用到fstream头文件,然后用ifstream就可以了。 我给LZ写一个例子,lz看一下就明白了: strcut stockType { string personAddress; string personCity; string personState; int personZIP; }; void addressType :: getData() { ifstream infile; int index; string inputFile; stockType addressTypeList[NO_OF_PERSON]; cout<<"Enter the file path: "; cin>>inputFile; //输入txt文件路径 cout<<endl; infile(inputFile.c_str()); //infile按照指定路径读取txt文件 for(index = 0; index < NO_OF_PERSON; index++)//读取txt文件至struct { infile>>addressTypeList[index].personAddress; infile>>addressTypeList[index].personCity; infile>>addressTypeList[index].personState; infile>>addressTypeList[index].personZIP; } }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-11-12
自已写个函数,函数意思为 按 什么分隔符分隔,用CFile打开,按回车符一行读进来,按空格符分隔每个值。
相似回答