正在将txt文件读取到链接列表中
Reading from txt file into a linked list
我正试图将*.txt文件中的数据读取到链接列表中。该文件由16种不同类型的数据组成,这些数据排列成行,每种数据类型之间都有选项卡。我对此有几个问题。编译器没有给出任何错误,但当我运行程序并输入要读取的文件名时,什么都不会发生。我试着用for循环for(int i=0; i<NUM_LINES; i++)
替换while(!din.eof())
行(下面的第27行),这似乎有帮助,但我得到了前三个元素(所有字符串类型),后面跟着一行零,这一行一直重复到循环结束。我已经包含了我认为问题所在的方法。我想知道是否有人可以看看我的代码,让我知道问题出在哪里。谢谢。
#include <iostream>
#include <fstream>
using namespace std;
class Node
{
public:
// Constructors
Node();
Node(const string name, const string position, const string team, const int g, const int att, const float attg, const int cmp, const float pct, const int yds, const float ydsg, const int lng, const int td, const int inte, const int sck, const int sckyl, const float rating);
Node(const Node & node);
~Node();
// Methods
bool readData();
void setNext(Node* next);
void print() const;
private:
string Name;
string Position;
string Team;
int G;
int Att;
float AttG;
int Cmp;
float Pct;
int Yds;
float YdsG;
int Lng;
int TD;
int Int;
int Sck;
int SckYL;
float Rating;
Node *Next;
};
//----------------------------------------------
// Constructor method
//----------------------------------------------
Node::Node()
{
Name = "";
Position = "";
Team = "";
G = 0;
Att = 0;
AttG = 0.0;
Cmp = 0;
Pct = 0.0;
Yds = 0;
YdsG = 0.0;
Lng = 0;
TD = 0;
Int = 0;
Sck = 0;
SckYL = 0;
Rating = 0.0;
Next = NULL;
}
//----------------------------------------------
// Constructor method with parameters
//----------------------------------------------
Node::Node(const string name, const string position, const string team, const int g, const int att, const float attg, const int cmp, const float pct, const int yds, const float ydsg, const int lng, const int td, const int inte, const int sck, const int sckyl, const float rating)
{
Name = name;
Position = position;
Team = team;
G = g;
Att = att;
AttG = attg;
Cmp = cmp;
Pct = pct;
Yds = yds;
YdsG = ydsg;
Lng = lng;
TD = td;
Int = inte;
Sck = sck;
SckYL = sckyl;
Rating = rating;
Next = NULL;
}
//----------------------------------------------
// Copy constructor method
//----------------------------------------------
Node::Node(const Node & node)
{
Name = node.Name;
Position = node.Position;
Team = node.Team;
G = node.G;
Att = node.Att;
AttG = node.AttG;
Cmp = node.Cmp;
Pct = node.Pct;
Yds = node.Yds;
YdsG = node.YdsG;
Lng = node.Lng;
TD = node.TD;
Int = node.Int;
Sck = node.Sck;
SckYL = node.SckYL;
Rating = node.Rating;
Next = NULL;
}
//----------------------------------------------
// Destructor method
//----------------------------------------------
Node::~Node()
{
}
//----------------------------------------------
// Method: readData
// Purpose: Read data from file into linked list, and print contents of the list.
//----------------------------------------------
bool Node::readData()
{
// Declare local variables
string name, position, team;
name = position = team = "";
int g, att, cmp, yds, lng, td, inte, sck, sckyl;
g = att = cmp = yds = lng = td = inte = sck = sckyl = 0;
float attg, pct, ydsg, rating;
attg = pct = ydsg = rating = 0.0;
// Get file name
string filename = "";
cout << "Enter file name: ";
cin >> filename;
// Open input file
ifstream din;
din.open(filename.c_str());
if (din.fail())
{
cerr << "Could not open file: " << filename << endl;
return false;
}
// Read data
Node *head = NULL;
while (!din.eof())
{
din >> name >> position >> team >> g >> att >> attg >> cmp >> pct >> yds >> ydsg >> lng >> td >> inte >> sck >> sckyl >> rating;
Node *temp = new Node(name, position, team, g, att, attg, cmp, pct, yds, ydsg, lng, td, inte, sck, sckyl, rating);
temp->setNext(head);
head = temp;
}
din.close();
head->print();
return true;
}
//----------------------------------------------
// setNext method
//----------------------------------------------
void Node::setNext(Node* next)
{
Next = next;
}
//----------------------------------------------
// Print method
//----------------------------------------------
void Node::print() const
{
cout << Name << " " << Position << " " << Team << " " << G << " " << Att << " " << AttG << " "
<< Cmp << " " << Pct << " " << Yds << " " << YdsG << " " << Lng << " " << TD << " " << Int << " "
<< Sck << " " << SckYL << " " << Rating;
if (Next != NULL)
Next->print();
}
//----------------------------------------------
// Main program
//----------------------------------------------
int main()
{
Node list;
if (list.readData())
cout << "Success" << endl;
else
cout << "Fail" << endl;
return 0;
}
在您的文件中,有Charlie Batch
等等。当你读到它时,你认为会发生什么?名称将变为Charlie
;位置将变为CCD_ 5。
之所以会发生这种情况,是因为在满足空白之前都会读取数据。
http://cplusplus.com/reference/fstream/fstream/?kw=fstream
你试过启动循环吗?获取文件的第一行,然后执行while语句。在while语句中,创建新节点,然后获取下一行。在那里结束循环的代码块。
// Read data
Node *head = NULL;
din >> name >> position >> team >> g >> att >> attg >> cmp >> pct >> yds >> ydsg >> lng >> td >> inte >> sck >> sckyl >> rating;
while (!din.eof())
{
Node *temp = new Node(name, position, team, g, att, attg, cmp, pct, yds, ydsg, lng, td, inte, sck, sckyl, rating);
temp->setNext(head);
head = temp;
din >> name >> position >> team >> g >> att >> attg >> cmp >> pct >> yds >> ydsg >> lng >> td >> inte >> sck >> sckyl >> rating;
}
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表