链接列表从文本文件
Linked List From Text File
我对指针和链表很陌生,但我正试图编写一个简单的程序,从文本文件读取数据到链表。输入函数有问题。它看起来像这样:
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:
如果我在genre
中读取的行正下方的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:
编辑:对不起,我明白了。这只是再忽略几个人的问题。谢谢。 它没有挂起,它正在等待您的输入,因为您有:
cin >> dvdPtr -> year; // read year from std input!!
cin >> dvdPtr -> rating;
函数CreateList
中的。这个函数打开用户指定的文件并从中读取DVD字段。
将以上行改为:
inFile >> dvdPtr -> year; // read year from file.
inFile >> dvdPtr -> rating;
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)