【问题标题】:Reading from txt file into a linked list从txt文件读入链表
【发布时间】:2013-02-08 05:46:15
【问题描述】:

我正在尝试将 *.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;
}

【问题讨论】:

  • 好吧,我看不出这段代码有什么问题。你确定你写的文件正确吗?
  • 如果您询问的是 txt 文件,那么可以。如果有帮助,我可以上传整个程序(尽管它将近 200 行)。
  • 好的。我已经编辑了我的原始帖子以包含整个程序。谢谢。
  • This 是我必须阅读的文件。
  • 16个参数的那个?

标签: c++ linked-list


【解决方案1】:

在你的文件中,Charlie Batch 等等。 当你读到它时,你认为会发生什么? 名称将变为Charlie;位置将变为Batch

发生这种情况是因为在遇到空格之前读取数据。

http://cplusplus.com/reference/fstream/fstream/?kw=fstream

【讨论】:

  • 是的,这似乎是问题所在。再次感谢您。
【解决方案2】:

您是否尝试过启动循环?获取文件的第一行,然后执行您的 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;

}

【讨论】:

  • 我已经这样做了,但我不确定它是否有帮助。当我输入文件名并按回车时,没有任何反应。如果我输入了无效的文件名,我会收到错误消息。另外,在主程序中,我只用readData(); 调用readData 方法。这对我来说似乎很奇怪,但在程序应该越过// Read data 行之前它似乎工作正常。
  • 忽略我上一条评论中的“也”部分。上面的代码应该是一个方法,而不是一个函数,但我通过将上面代码的第一行更改为bool Node::readData() 解决了这个问题。不过,我仍然无法弄清楚为什么什么也没发生。再次感谢。
  • 如果在第一次读取文件后立即设置断点,每个变量中存储的内容是什么?
  • 对不起,我不知道如何设置断点。老实说,我以前从未听说过。
  • 好的,在第一次读取后执行一个打印 16 个变量的 cout。然后,执行此行: system("pause");这将向您显示每个变量所持有的内容并暂停以查看结果。它将为我们提供有关问题是文件格式还是代码问题的线索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2015-12-13
相关资源
最近更新 更多