c++ - 来自文本文件的链表

标签 c++ data-structures file-io linked-list

我对指针和链表非常陌生,但我正在尝试编写一个简单的程序,将数据从文本文件读入链表。我在使用输入功能时遇到问题。它看起来像这样:

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;
}

我还有一个如下所示的输出函数:

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();

}

主要代码如下:

// 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);

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

预先感谢您的帮助。

编辑:
所以我解决了 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.

不胜枚举,大约有 10 部这种格式的电影。但是,运行程序后,输出文件如下所示:

Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 

如果我将 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: 

编辑:对不起,我想通了。只是多了几个无视的问题。谢谢。

最佳答案

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

cin >> dvdPtr -> year;   // read year from std input!!
cin >> dvdPtr -> rating;

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

将上面几行改为:

inFile >> dvdPtr -> year;   // read year from file.
inFile >> dvdPtr -> rating;

关于c++ - 来自文本文件的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5632902/

相关文章:

algorithm - 查找给定开始和结束时间的并发事件数

data-structures - 在 Go 中使用 TTL 选项映射

c# - .Net 中的文件操作是否有异步替代方案?

python - 如何通过 python 调用/运行软件?

C++类实例声明无匹配函数

c++ - 我的第二次 C++ 尝试和使用变量的问题

algorithm - 在字典中查找作为给定字符串子序列的最长单词(Google TechDevGuide)

Java读取文件夹和子文件夹中的文件

c++ - 在 Windows 7 上安装 Qt 5.5

c++ - 严格指针别名 : any solution for a specific problem?