将文本文件读入链表
Reading text file into linked list
我正在开发一个程序,该程序需要我读取文本文件并将数据插入链表中。我很难将数据读入代码编译的链表中,但输出中没有显示任何内容。任何帮助或建议将不胜感激。
文本文件格式如下:字符串 int
int int int int intA 83 140 228 286 426 612 486 577 836 0 0 Aaliyah
0 0 0 0 0 0 0 0 0 0 380 215亚伦 193 208 218 274 279 232 132 36 32 31 41
修道院 0 0 0 0 0 00 0 537 451 428
对内部数据结构使用链表。
linked_list<NameSurferEntry> database;
这是我用来解析文件中每一行的函数。
NameSurferEntry::NameSurferEntry(string line){
vector<string> filelines;
stringstream inputstringstream;
year.push_back(1900);
year.push_back(1910);
year.push_back(1920);
year.push_back(1930);
year.push_back(1940);
year.push_back(1950);
year.push_back(1960);
year.push_back(1970);
year.push_back(1980);
year.push_back(1990);
year.push_back(2000);
year.push_back(2010);
while(getline(inputstringstream, line,' ')){
filelines.push_back(line);
}
for(unsigned int i=1; i < filelines.size(); i++){
cout << year.at(i-1) << " " << filelines.at(0 + 1) << "n";
}
cout << " " << endl;
}
string NameSurferEntry::getName(){
return name;
}
int NameSurferEntry::getRank(int decade){
return year.at(decade);
}
这是我用来读取文本文件的函数。
NameSurferDataBase::NameSurferDataBase(string filename){
ifstream input;
input.open(filename);
if(!input.is_open()){
cout << "Not Open";
}else{
string temp;
while(!input.eof()){
getline(input,temp);
if(!input.eof()){
NameSurferEntry entry(temp);
database.InsertInOrder(entry);
}
}
}
}
按顺序排列:
template <class NODETYPE>
bool linked_list<NODETYPE>::InsertInOrder(NODETYPE value)
{
if (IsEmpty()) {
// A variable used to point to data that is being inserted.
ListNode<NODETYPE> *temp = new ListNode<NODETYPE>;
if (temp == NULL) return false;
temp->data = value;
first = last = current = temp;
last->next = NULL;
}
else if (value >= last->data) {
return InsertRear(value);
}
else if (first->data >= value) {
return InsertFront(value);
}
else {
// A variable used to point to data that is being inserted.
ListNode<NODETYPE> *temp = new ListNode<NODETYPE>;
if (temp == NULL) return false;
temp->data = value;
current = first;
while (current->next->data < temp->data) {
current = current->next;
}
temp->next = current->next;
current->next = temp;
return true;
}
return false;
}
查看您提供的以下代码:
在 NameSurferDatabase 中:
while(!input.eof()){
getline(input,temp);
if(!input.eof()){
NameSurferEntry entry(temp);
database.InsertInOrder(entry);
}
您正在正确读取文件中的 1 行并将其放入名为"temp"的字符串中。然后,您将"temp"字符串传递给NameSurferEntry。您现在希望此方法剖析"临时"字符串。
但是,在 NameSurferEntry 中,您键入了以下内容:
stringstream inputstringstream;
while(getline(inputstringstream, line,' ')){
filelines.push_back(line);
}
您正在声明一个字符串流,默认情况下该字符串流将为空。然后你做了一个while循环来做getline(inputstringstream,line,''(,它将从inputstringstream中获取1行并将其放入变量"line"。这会将变量"line"的内容替换为任何内容。
以下是解决方法:
stringstream inputstringstream;
inputstringstream >> line;
string temp;
while(inputstringstream << temp){
filelines.push_back(temp);
}
相关文章:
- 文本文件中的单词链表
- 将从格式化文本文件读取的文本数据存储到链表
- 从预制文件制作链表
- 如何从文件中检索链表(在 Turbo C++ 中)
- 在排序链表中插入结构并将其写入二进制文件
- 从文本文件加载链表并打印时无限循环
- 如何从文件读取到动态分配的指向结构的指针链表中?
- 在自定义哈希表头文件中包括自定义链表
- 将文本文件读入链表
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 从文件读取到链表中
- 将文本文件读取到双向链表并将其输出 - C++
- 链表堆栈头文件中"<"标记之前的预期初始值设定项
- 将结构从二进制文件读取到链表,导致无限循环
- 从文本文件将整数读入简单的链表中.然后对整数列表进行气泡排序并读出到另一个文件
- C++ 链表类 -> 头文件
- 从文件读取链表C++从 void* 到 char* 的转换无效
- 使用c++通过文件存储和检索链表
- 想要读取超过 50,000 个 TXT 文件并将它们保存在 C++ 的链表中
- 如何将文件中的数字插入到链表中