首页 > 解决方案 > 读取文件中的多种数据类型并将其存储在链表中 C++

问题描述

如何读取文件中的多种数据类型并将其存储在链表中。我有下面显示的代码,但它通常读取没有链表的文件,但是当我使用链表时它不起作用。我认为我的代码有问题。编程语言:C++ 希望你能帮助我如何做到这一点。谢谢

struct Video {
int videoID;
string movieTitle;
string movieGenre;
string movieProduction;
int numberOfCopies;};

class videoList {
private:
    struct videoNode {
        Video video;
        struct videoNode *next;
    };
    videoNode *head;

public:
    videoList(); //constructor
    //~videoList(); //deconstructor
    void insertNewVideo (Video vid);
    void rentVideo (Video vid);
    void returnVideo (Video vid);
    void showVideoDetails (Video vid);
    void displayVideo ();
    bool isVideoExist();
    void exit ();
    void generateVideoID();
    void readVideoFile();};

void videoList::readVideoFile () {
videoNode *nodePtr;
ifstream file( "videoList.txt" ) ;
string line ;
string id, movieTitle, movieGenre, movieProduction, numberOfCopies;

if (!file) {
    cout << endl << "\t\t\t\t There is file!" << endl;
}
else {
    std::stringstream linestream(line);
    cout << endl << "\t\t\t\t Movie List " << endl; 
    nodePtr = head;

    while (nodePtr)
        while (getline(file, line)){
{


    std::getline(linestream, id, ',');
    std::getline(linestream, nodePtr->video.movieTitle, ',');
    std::getline(linestream, nodePtr->video.movieGenre, ',');
    std::getline(linestream, nodePtr->video.movieProduction, ',');
    linestream >> nodePtr->video.numberOfCopies;

    if(linestream) // true if there were no errors reading the stream
    {
        std::cout << id << '\n';
        std::cout << nodePtr->video.movieTitle << '\n';
        std::cout << nodePtr->video.movieGenre << '\n';
        std::cout << nodePtr->video.movieProduction << '\n';
        std::cout << nodePtr->video.movieTitle << '\n';
    }
}}}}

标签: c++

解决方案


推荐阅读