您好我需要读取文件并从文件到结构数组获取数据。
结构
struct Activity {
string ID;
string Name;
string quantity; };
我有这个从文件中读取的功能
int* fillStructure(ifstream &fileActivity){
int i=0;
int numberOfElements = numberOfLines(fileActivity);
Activity* myActivity = new Activity[numberOfElements];
while (i < numberOfElements)
{
getline(fileActivity, myAktivity[i].ID, ',');
getline(fileActivity, myActivity[i].Name, ',');
getline(fileActivity, myActivity[i].quantity, '\n');
i++;
}
fileActivity.close();
return myActivity; }
当我尝试在main函数中打印结构成员时它不起作用
int main(){
if (!(fileActivity.is_open())){
cout << "Error when reading file" << endl;
return 0;
}
fillStructure(fileActivity);
cout << myActivity[1].ID << endl; return 0; }
我是初学者,你能帮我解决我做错的事吗?
答案 0 :(得分:3)
您在myActivity
中声明了void fillStructure(ifstream &fileActivity)
,但尝试从int main()
进行访问。
答案 1 :(得分:0)
您必须在main函数中声明返回值。
# From Paul Eggert (2014-09-06):
# Monthly Notices of the Royal Astronomical Society 44, 4 (1884-02-08), 208
# says that New York City Hall time was 3 minutes 58.4 seconds fast of
# Eastern time (i.e., -4:56:01.6) just before the 1883 switch. Round to the
# nearest second.
在 main 函数中声明 fillStructure 函数的返回类型,如下所示:
Zone America/New_York -4:56:02 - LMT 1883 Nov 18 12:03:58
此代码段适用于我