我一直在研究链表,并试图从文件中读取一个字符串并将其存储在一个节点中。 [英] I have been working on linked list and was trying to read a string from file and store it in a node.

查看:69
本文介绍了我一直在研究链表,并试图从文件中读取一个字符串并将其存储在一个节点中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在尝试从保存在同一项目文件夹中的文件file.txt中读取每个字符串(word),其中包含一些名称。这是我制作的代码。实际上尝试做的是尝试阅读每个单词来自文件并存储每个节点中的每个单词,对于下一个单词,将创建新节点,它将在其中存储下一个单词并一直持续到文件末尾。但是当它到达临时 - 点时> next = headprogram crashes.any idea我怎样才能修复它。



我尝试过:



  #include   <   iostream  >  
#include < stdio.h >
< span class =code-keyword> #include < iomanip >
#include < conio.h > ;
#include < ; string >
#include < fstream >

使用 namespace std;

struct 节点
{
字符串数据;
node * next;
};

int main()
{
ifstream fout;

node * head = NULL;
字符串名称;
int counter = 1 ;

fout.open( file.txt);
if (fout.is_open())
{
while (getline(fout,name))
{
cout<< 在数据处插入。<< ENDL;
cout<< 输入data =<<名称;
node * temp;
temp =(node *)malloc( sizeof (node));
head = temp;
temp-> data = name;
temp-> next = head;
cout<< DATA ENTERED。<< ENDL;
cout<< NUMEMENT OF ELEMENTS =<< counter ++<< ENDL;
}
}

_getch();
}

解决方案

嗯。

看看你的代码做了什么:

 node * temp; 
temp =(node *)malloc( sizeof (node));

声明速度并将其设置为新节点。< pre lang =c ++> head = temp;

将头设置为新节点。

 temp-> data = name; 

Save数据。

 temp-> next = head; 

next 节点设置为头部 - 现在是同一个节点!

因此,每次创建新节点时,都会丢弃上一个节点,并且最终会得到一个节点的循环列表!



这不是你注意到的问题,但它会很快影响你。

至于你注意到的那个:我们可以在相同的情况下运行它 - 我们没有权限访问您的文件。所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


我欠了一个List类来处理项目intertion和内存清理。例如

  #include   <   iostream  >  
< span class =code-keyword> #include < fstream >
使用 namespace std;

struct 节点
{
节点( const string& name):name(name),next( nullptr ){}
string name;
Node * next;
};

class 列出
{
节点*头;
public
List():head( nullptr ){}
~List();
void add( const string& name);
朋友 ostream& operator << (ostream& os, const List& l);
private
void delete_last();
};

void List :: delete_last()
{
Node * cur = head;
if (!cur) return ;
if (!cur-> next)
{
delete cur;
head = nullptr ;
return ;
}

while (cur-> next-> next)
{
cur = CUR->接着,
}
delete (cur-> next);
cur-> next = nullptr ;
}

List :: ~List()
{
while (head)
delete_last();
}
void List :: add( const string& name)
{
if (!head)
{
head = new 节点(名称);
return ;
}

节点* cur = head;

while (cur-> next)
cur = cur-> next;

cur-> next = new 节点(名称);
}

ostream& operator << (ostream& os, const List& l)
{
Node * cur = l.head;
while (cur)
{
os<< << CUR->名称;
cur = cur-> next;
}
return os;
}

int main()
{
List l;
字符串名称;

ifstream ifs( file.txt);

while (getline(ifs,name))
{
l.add(name);
}

cout<< l<< ENDL;
}





请注意:仅作为链接列表上的练习。在真实世界的应用程序中,使用向量,会好得多:

  #include   <   iostream  >  
#include < fstream >
#include < vector >
使用 命名空间标准;

int main()
{
vector< string> v;
ifstream ifs( file.txt);
字符串名称;

while (getline(ifs,name))
v.push_back(name);

for const auto & s:v)
cout<< s<< ;

cout<< ENDL;
}


lately i have been trying to read each string(word) from a file "file.txt" saved in the same project folder with some names in it.here's the code i made.what actually trying to do is trying to read each word from file and store each word in each node and for the next word,new node would be created and it would store the next word in it and keeps going until at the end of file.but when it reaches the point "temp->next = head" program crashes.any idea how can i get it fixed.

What I have tried:

#include<iostream>
		#include<stdio.h>
		#include<iomanip>
		#include<conio.h>
		#include<string>
		#include<fstream>

		using namespace std;

		struct node
		{
			string data;
			node* next;
		};

		int main()
		{
			ifstream fout;

			node *head = NULL;
			string name;
			int  counter = 1;

			fout.open("file.txt");
			if (fout.is_open())
			{
				while (getline(fout, name))
				{
					cout << "Inserting at data." << endl;
					cout << "enter the data = " << name;
					node *temp;
					temp = (node*)malloc(sizeof(node));
					head = temp;
					temp->data = name;
					temp->next = head;
					cout << "DATA ENTERED." << endl;
					cout << "NUMBER OF ELEMENTS = " << counter++ << endl;
				}
			}

			_getch();
		}

解决方案

Um.
Look at what your code does:

node *temp;
temp = (node*)malloc(sizeof(node));

Declare tempo and set it to a new node.

head = temp;

Set the head to the new node.

temp->data = name;

Save the data.

temp->next = head;

Set the next node to the head - which is now the same node!
So each time you create a new node, you throw away the previous node, and you will always end up with a circular list of one node!

That isn't the problem you have noticed, but it will affect you fairly soon.
As for the one you have noticed: we can;t run it under teh same circumstances - we don;t have access to your file. So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


I owuld make a List class for handling items intertion and memory cleanup. For instance

#include <iostream>
#include <fstream>
using namespace std;

struct Node
{
  Node( const string & name ):name(name),next(nullptr){}
  string name;
  Node * next;
};

class List
{
  Node * head;
public:
  List():head(nullptr){}
  ~List();
  void add(const string & name);
  friend ostream & operator << (ostream & os, const List & l);
private:
  void delete_last();
};

void List::delete_last()
{
  Node * cur = head;
  if ( ! cur ) return;
  if ( ! cur->next )
  {
    delete cur;
    head = nullptr;
    return;
  }

  while ( cur->next->next )
  {
    cur = cur->next;
  }
  delete (cur->next);
  cur->next = nullptr;
}

List::~List()
{
  while ( head )
    delete_last();
}
void List::add( const string & name)
{
  if ( ! head )
  {
    head = new Node(name);
    return;
  }

  Node * cur = head;

  while ( cur->next )
    cur = cur->next;

  cur->next = new Node(name);
}

ostream & operator << (ostream & os, const List & l)
{
  Node * cur = l.head;
  while (cur)
  {
    os << " " << cur->name;
    cur = cur->next;
  }
  return os;
}

int main()
{
  List l;
  string name;

  ifstream ifs("file.txt");

  while ( getline( ifs, name) )
  {
    l.add( name );
  }

  cout << l << endl;
}



Please note: Do that only as an exercise on linked lists. In a real world application, using a vector, would be far better:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
  vector <string> v;
  ifstream ifs("file.txt");
  string name;

  while ( getline(ifs, name) )
    v.push_back(name);

  for ( const  auto & s : v)
    cout << s << " ";

  cout << endl;
}


这篇关于我一直在研究链表,并试图从文件中读取一个字符串并将其存储在一个节点中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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