Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将子目录的名称存储为列表中的节点时出错_C++_List_Directory - Fatal编程技术网

C++ 将子目录的名称存储为列表中的节点时出错

C++ 将子目录的名称存储为列表中的节点时出错,c++,list,directory,C++,List,Directory,我打开一个目录,其中有多个子目录,其中包含国家名称。我想要的是制作一个列表,并将这些目录的路径存储在节点中。当我打印列表和析构函数时,问题出现了。我似乎不明白哪里出了问题,因为我以前多次使用这些函数,没有出现任何问题 目录如下所示: input_dir >Tanzania >Honduras >iTaiwan >Albania >Qatar >Grenada >Thailand >Croatia

我打开一个目录,其中有多个子目录,其中包含国家名称。我想要的是制作一个列表,并将这些目录的路径存储在节点中。当我打印列表和析构函数时,问题出现了。我似乎不明白哪里出了问题,因为我以前多次使用这些函数,没有出现任何问题

目录如下所示:

input_dir
   >Tanzania
   >Honduras
   >iTaiwan
   >Albania
   >Qatar
   >Grenada
   >Thailand
   >Croatia
   >Guatemala
   >Uruguay
以下是所有代码: 我的主要意见是:

以下是打开目录并将路径存储在列表中的函数:

void fetch_countries( int num_monitors,  Countries& all_countries, char* input_dir_path)
{
  DIR * input_dir = opendir(input_dir_path); // open the path

  if(input_dir == NULL){
    cout<< "Error opening directory" << endl;
    exit(1);
  }

  char buffer[257];
  struct dirent *entry;  // for the directory entries
  
  while ((entry = readdir(input_dir)) != NULL)
  {
    char *f_name = entry->d_name;
    if (entry->d_type != DT_DIR || !strcmp(f_name, ".") || !strcmp(f_name, ".."))  // Ignore . and .. dirs
      continue;
    // buffer contains instance: input_dir/Uruguay
    sprintf(buffer, "%s/%s", input_dir_path, f_name);
    all_countries.Insertion(buffer);  // storing all of the countries on a list
  }
  closedir(input_dir);
}

有什么我没看见的吗?如果您从未将
newnode->next
设置为
NULL
,将不胜感激。因此,当您在节点列表中循环时,当您到达最后一个节点时,会遇到一个统一化的指针

您可以更改
CountryNode
构造函数以将其初始化为此默认值

CountryNode::CountryNode(const string country)
    :country(country), next(NULL)
{}

为了节省别人评论的时间,我不打算使用stl“我不打算使用stl”任何有弹性的推理为什么不呢?如果是,请回答您的问题以添加该信息。另外:您从未在
Insertion()中设置
newNode->next
,我也不会使用STL。现在是90年代。C++标准库,另一方面……YUP,这是问题…非常感谢你,祝你有一个美好的一天
class CountryNode
{
public:
    string country;
    CountryNode* next;
    CountryNode(const string country);
    ~CountryNode();
};

class Countries
{
public:
    CountryNode* head;
    CountryNode* tail;
    Countries();
    ~Countries();
    void Insertion(const string country);
    void print();
};

void Countries::Insertion(const string country)
{
  CountryNode* newnode = new CountryNode(country);

  //the list is empty
  if(head == NULL)
  {
    head = newnode;
    tail = newnode;
    return;
  }
  tail->next = newnode;
  tail = newnode;
}

Countries::Countries()
{
  head = NULL;
  tail = NULL;
}

void Countries::print()
{ // now we print the nodes
  CountryNode* current;
  current=head;
  while (current != NULL)
  {
    cout << current->country << endl;
    if (current->next != NULL)
      cout << "->";
    current = current->next;
  }
  cout << endl;
}

Countries::~Countries()
{
  CountryNode* current = head;
  CountryNode* next;

  while (current != NULL)
  {
    next = current->next;
    delete current;
    current = next;
  }
  
  head = NULL;
  tail = NULL;
}

CountryNode::CountryNode(const string country)
:country(country)
{}

CountryNode::~CountryNode() {}
input_dir/Tanzania
->input_dir/Honduras
->input_dir/Taiwan
->input_dir/Albania
->input_dir/Qatar
->input_dir/Grenada
->input_dir/Thailand
->input_dir/Croatia
->input_dir/Guatemala
->input_dir/Uruguay
==16098== Conditional jump or move depends on uninitialised value(s)
==16098==    at 0x10BA29: Countries::print() (in /home/...)
==16098==    by 0x10A8F4: main (in /home/...)
==16098== 
==16098== Conditional jump or move depends on uninitialised value(s)
==16098==    at 0x10B9F4: Countries::print() (in /home/...)
==16098==    by 0x10A8F4: main (in /home/...)
==16098== 

==16098== Conditional jump or move depends on uninitialised value(s)
==16098==    at 0x10BA87: Countries::~Countries() (in /home/...)
==16098==    by 0x10A980: main (in /home/....)
==16098== 
==16098== 
==16098== HEAP SUMMARY:
==16098==     in use at exit: 0 bytes in 0 blocks
==16098==   total heap usage: 533 allocs, 533 frees, 153,121 bytes allocated
==16098== 
==16098== All heap blocks were freed -- no leaks are possible
==16098== 
==16098== Use --track-origins=yes to see where uninitialised values come from
==16098== For lists of detected and suppressed errors, rerun with: -s
==16098== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
CountryNode::CountryNode(const string country)
    :country(country), next(NULL)
{}