Linked List From Text File

六月ゝ 毕业季﹏ 提交于 2020-01-05 07:45:29

问题


I'm very new to pointers and linked lists but I'm trying to write a simple program that reads in data from a text file into a linked list. I'm having trouble with the input function. It looks like this:

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

I also have an output function that looks like this:

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

}

Here's what the main code looks like:

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

I'm sorry if this is a stupid question, I can't find any good tutorials online about linked lists and I don't really understand why this just doesn't do anything after I type in the Input File Name.

Thanks in advance for your help.

EDIT:
So I fixed the cin problem, but now there's a different problem. When my input file looks like this:

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.

The list goes on, there are about 10 movies in this format. However, after running the program, the output file looks like this:

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

If I add an inFile.ignore(100, '\n'); to the CreateList function right below the line where I read in genre, than the output looks like this:

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: 

EDIT: Sorry, I figured it out. It was just a matter of a few more ignores. Thanks.


回答1:


It is not hanging, it is waiting for your input because you have:

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

in the function CreateList. This function opens the user specified file and reads the DVD fields from it.

Change the above lines to:

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


来源:https://stackoverflow.com/questions/5632902/linked-list-from-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!