从文本文件链接列表 [英] Linked List From Text File

查看:161
本文介绍了从文本文件链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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 - > supportsActor);
getline(inFile,dvdPtr - >类型);
cin>> dvdPtr - >年;
cin>> dvdPtr - >评分;
getline(inFile,dvdPtr - >概要);

dvdPtr - >下一个=头
head = dvdPtr;
dvdPtr = new DVDNode;
}

删除dvdPtr;
inFile.close();

返回头;
}

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

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

DVDNode * dvdPtr;
dvdPtr = head;

while(outFile&& dvdPtr!= NULL)
{
outFile<< dvdPtr - >标题;
outFile<<< dvdPtr - >主演;
outFile<<< dvdPtr - >男配角;
outFile<<< dvdPtr - >类型;
outFile<<< dvdPtr - >年;
outFile<<< dvdPtr - >评分;
outFile<<< dvdPtr - >概要;

dvdPtr = dvdPtr - >下一个;
}

outFile.close();

}

主要代码如下:

  //变量
string inputFile;
string outputFile;
DVDNode * head;

//输入
cout<<< 输入输入文件的名称:;
getline(cin,inputFile);

head = CreateList(inputFile);

//输出
cout<<<输入输出文件的名称:;
getline(cin,outputFile);

OutputList(head,outputFile);

很抱歉,如果这是一个愚蠢的问题,我在网上找不到任何好的教程链接列表,我真的不明白为什么这只是在输入文件名后输入任何内容。



提前感谢您的帮助。



编辑:

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

  Yankee Doodle Dandee 
James Cagney
Joan Leslie
音乐
传记
1942
8
这部电影描绘了着名的音乐作曲家,剧作家,演员,舞者和歌手乔治·科汉的生活。

X-Men
休·杰克曼
帕特里克·斯图尔特
动作
动作
2000
7
星球,不寻常的孩子出生时对其遗传密码有更多的扭曲。

列表继续,这个格式有大约10部电影。但是,运行程序后,输出文件如下所示:

 标题:Yankee Doodle Dandee 
主角: James Cagney
支持演员:Joan Leslie
类型:音乐
年份:1735357008
评分:544039282
剧情介绍:
/ pre>

如果我在File.ignore(100,'\\\
'); 中添加一个 code> CreateList 功能正好位于我在类型中阅读的行,比输出如下所示:

 标题:这部电影描绘了着名的音乐作曲家,剧作家,演员,舞者和歌手乔治·科汉的生活。 
主角:
支持演员:X-Men
类型:休·杰克曼
年份:0
评分:0
简介:
标题:Yankee Doodle Dandee
主角:James Cagney
支持演员:Joan Leslie
类型:音乐
年份:1942
评分:8
简介:

编辑:对不起,我想出来了。这只是一个更多的忽视的问题。谢谢。

解决方案

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

  cin>> dvdPtr  - >年; //从std输入读取年份! 
cin>> dvdPtr - >评分;

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



将以上行更改为:

  inFile>> dvdPtr  - >年; //从文件读取年份。 
inFile>> dvdPtr - >评分;


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.

解决方案

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;

这篇关于从文本文件链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆