【问题标题】:creating a struct for file/directory tree [closed]为文件/目录树创建结构[关闭]
【发布时间】:2024-04-23 01:20:02
【问题描述】:

如何为文件/目录树创建结构。 c 程序获取一个 txt 文件输入,其中包含每个 txt 文件路径的 shell 脚本。例如

a\a1.txt
a\m\m1.txt

你将如何为此创建一个结构?

【问题讨论】:

  • 首先使用struct关键字。
  • 结构体是一种组织数据的方式。您的程序将如何处理从输入文件中读取的数据?
  • 用户运行C程序并输入一个文本文件的名称,它应该搜索该文件

标签: c shell file-io


【解决方案1】:

也许

对于一个简单的一维字符串

struct MyPath {
    char *element;  // Pointer to the string of one part.
    MyPath *next;   // Pointer to the next part - NULL if none.
}

对于完整的二叉树表示

struct Node {
   char *element; // Pointer to the string - node.
   Node *left;    // Pointer to the left subtree.
   Node *right;   // Pointer to the right subtree.
}

【讨论】: