将数据从txt文件存储到结构数组中

时间:2018-04-16 20:04:46

标签: c++ file struct

#include <iostream>
#include <string.h>
#include <string>
#include<fstream>
struct time
{
    int hours, minutes;
};

struct moviedata
{
    string moviename, genre, actorname1, actorname2;
    int yearreleased, priceperday;
    time duration;

};

void readmoviedata(moviedata *ptr) /*not ale to run this code*/
{
    int i = 0;
    string x;
    ifstream inside("movies.txt");
    while (!inside.eof())
    {

        getline(inside, ptr[i].moviename);
        inside >> ptr[i].yearreleased;
        getline(inside, ptr[i].genre);
        inside >> ptr[i].duration.hours;
        inside >> ptr[i].duration.minutes;
        getline(inside, ptr[i].actorname1);
        getline(inside, ptr[i].actorname2);
        inside >> ptr[i].yearreleased;
        i++;
    }
}
int main()
{
    moviedata *md = new moviedata;

    readmoviedata(md);
    delete md;
    return 0;
}

任何人都可以告诉我我的功能readmoviedata我做错了什么 一旦调用函数readmoviedata,就会逐行运行$ $ black grid出现,光标闪烁,其他

1 个答案:

答案 0 :(得分:0)

问题是你正在创建struct类型的一个指针变量。

moviedata *md = new moviedata;

并且您正在将此变量视为数组。

 ptr[i].yearreleased;