文本文件中的链接列表

Mic*_*ael 1 c++ file-io linked-list data-structures

我是指针和链表的新手,但我正在尝试编写一个简单的程序,将文本文件中的数据读入链表.我输入功能有问题.它看起来像这样:

DVDNode* CreateList(string fileName)
{
    ifstream inFile;
    inFile.open(fileName.c_str());

    DVDNode* head = NULL;
    DVDNode* dvdPtr;
    dvdPtr = new DVDNode;

    while(inFile && dvdPtr != NULL)
    {
        getline(inFile, dvdPtr -> title);
        getline(inFile, dvdPtr -> leadActor);
        getline(inFile, dvdPtr -> supportingActor);
        getline(inFile, dvdPtr -> genre);
        cin >> dvdPtr -> year;
        cin >> dvdPtr -> rating;
        getline(inFile, dvdPtr -> synopsis);

        dvdPtr -> next = head;
        head = dvdPtr;
        dvdPtr = new DVDNode;
    }

    delete dvdPtr;
    inFile.close();

    return head;
}
Run Code Online (Sandbox Code Playgroud)

我也有一个输出函数,如下所示:

void OutputList(DVDNode* head, string outputFile)
{
    ofstream outFile;
    outFile.open(outputFile.c_str());

    DVDNode* dvdPtr;
    dvdPtr = head;

    while(outFile && dvdPtr != NULL)
    {
        outFile << dvdPtr -> title;
        outFile << dvdPtr -> leadActor;
        outFile << dvdPtr -> supportingActor;
        outFile << dvdPtr -> genre;
        outFile << dvdPtr -> year;
        outFile << dvdPtr -> rating;
        outFile << dvdPtr -> synopsis;

        dvdPtr = dvdPtr -> next;
    }

    outFile.close();

}
Run Code Online (Sandbox Code Playgroud)

这是主要代码的样子:

// Variables
string inputFile;
string outputFile;
DVDNode* head;

// Input
cout << "Enter the name of the input file: ";
getline(cin, inputFile);

head = CreateList(inputFile);

// Output
cout << "Enter the name of the output file: ";
getline(cin, outputFile);

OutputList(head, outputFile);
Run Code Online (Sandbox Code Playgroud)

我很抱歉,如果这是一个愚蠢的问题,我在网上找不到任何关于链表的好教程,我真的不明白为什么在我输入输入文件名后这没有做任何事情.

在此先感谢您的帮助.

编辑:
所以我修复了cin问题,但现在有一个不同的问题.当我的输入文件如下所示:

Yankee Doodle Dandee
James Cagney
Joan Leslie
Musical
Biography
1942
8
This film depicts the life of the renowned musical composer, playwright, actor, dancer and singer George M. Cohan.

X-Men
Hugh Jackman
Patrick Stewart
Action
Action
2000
7
All over the planet, unusual children are born with an added twist to their genetic code.
Run Code Online (Sandbox Code Playgroud)

名单还在继续,大约有10部这种格式的电影.但是,运行该程序后,输出文件如下所示:

Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 
Run Code Online (Sandbox Code Playgroud)

如果我添加inFile.ignore(100, '\n');CreateList右边,我读线下功能genre,比输出看起来是这样的:

Title: This film depicts the life of the renowned musical composer, playwright, actor, dancer and singer George M. Cohan.
Lead Actor: 
Supporting Actor: X-Men
Genre: Hugh Jackman
Year: 0
Rating: 0
Synopsis: 
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1942
Rating: 8
Synopsis: 
Run Code Online (Sandbox Code Playgroud)

编辑:对不起,我明白了.这只是一些忽视的问题.谢谢.

cod*_*ict 6

它没有挂起,它正在等待您的输入,因为您有:

cin >> dvdPtr -> year;   // read year from std input!!
cin >> dvdPtr -> rating;
Run Code Online (Sandbox Code Playgroud)

在功能中CreateList.此函数打开用户指定的文件并从中读取DVD字段.

将以上行更改为:

inFile >> dvdPtr -> year;   // read year from file.
inFile >> dvdPtr -> rating;
Run Code Online (Sandbox Code Playgroud)