C++ 文本文件中的链接列表

C++ 文本文件中的链接列表,c++,data-structures,file-io,linked-list,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 &a

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

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

}
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.
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 
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: 
我还有一个输出函数,如下所示:

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

}
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.
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 
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: 
名单上还有,大约有10部这种格式的电影。但是,运行程序后,输出文件如下所示:

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

}
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.
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 
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: 
如果我添加一个
infle.ignore(100,“\n”)
到我在
流派
中读取的行正下方的
CreateList
函数,输出如下所示:

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

}
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.
Title: Yankee Doodle Dandee
Lead Actor: James Cagney
Supporting Actor: Joan Leslie
Genre: Musical
Year: 1735357008
Rating: 544039282
Synopsis: 
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;

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

cin >> dvdPtr -> year;   // read year from std input!!
cin >> dvdPtr -> rating;
在函数
CreateList
中。此函数用于打开用户指定的文件并从中读取DVD字段

将上述行更改为:

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

你说什么都不做是什么意思?你是说它挂了吗?是的,很抱歉。它只是挂着。我试着在输入函数的while循环中放置测试cout,但它们没有输出。你知道你也在while循环中读取cin吗?而且,我从来都不是一个真正的streams man,但是if(infle)检查文件是否在结尾,还是只检查文件是否打开,如果是后者,你永远不会退出while循环你能比“什么都不做”更具体一点吗?这不仅仅是因为它正在等待
CreateList
中的
cin>
语句中的输入?你可能想看看。你说什么都不做是什么意思?你是说它挂了吗?是的,很抱歉。它只是挂着。我试着在输入函数的while循环中放置测试cout,但它们没有输出。你知道你也在while循环中读取cin吗?而且,我从来都不是一个真正的streams man,但是if(infle)检查文件是否在结尾,还是只检查文件是否打开,如果是后者,你永远不会退出while循环你能比“什么都不做”更具体一点吗?这不仅仅是因为它正在等待
CreateList
中的
cin>
语句中的输入?你可能想看看。