【问题标题】:Linked List From Text File来自文本文件的链表
【发布时间】:2011-04-12 09:14:39
【问题描述】:

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

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: 

如果我在CreateList 函数的正下方添加一个inFile.ignore(100, '\n');,则输出将如下所示:

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: 

编辑:对不起,我想通了。这只是更多忽略的问题。谢谢。

【问题讨论】:

  • 什么都不做是什么意思?你的意思是它挂了?
  • 是的,很抱歉。它只是挂起。我尝试将测试 couts 放在输入函数的 while 循环中,但它们没有输出。
  • 你知道你也在 while 循环中读取 cin 吗?
  • 另外,我从来都不是流人,但 if(inFile) 会检查文件是否在末尾,还是只检查文件是否打开,如果是后者, 你永远不会退出 while 循环
  • 你能比“什么都不做”更具体吗?不只是它在等待CreateList 中的cin &gt;&gt; 语句中的输入吗?你可能想看看this

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


【解决方案1】:

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

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

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

将以上行改为:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多