c - 如何打印出每个目录但作为路径名?(在身体描述中解释)
问题描述
这是由 Lloyd Macrohon 编写的代码,所有功劳都属于他,但是在过去的两天里,我一直在尝试修改此代码,以便我不想显示目录中每个项目的列表,而是要修改它将每个项目显示为长路径名。
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}
int main(void) {
listdir(".", 0);
return 0;
}
以上是在 unix 终端中运行时会输出如下内容的原始代码:
-file
[directory]
[directory]
-file
-file
-file
....
但相反,我试图像这样运行它:
file
directory/directory/file
directory/file
directory/file
...
我的代码版本已经删除了意图,并用一个字符替换了它们,该字符包含一个字符,该字符应该是文件之前的路径名。
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listdir(const char *name,const char *pname)
{
DIR *dir;
struct dirent *entry;
char pathn = pname;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
//printf("%s/", entry->d_name);
pathn = pathn + entry->d_name;
listdir(path,pathn);
}
else if( pathn != ""){
printf("%s and %s ", pathn, entry->d_name);
}
else {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
}
int main(void) {
listdir(".","");
return 0;
}
注意:另外请原谅我可能错过的任何规定,我不知道未经他们许可修改/上传其他用户代码是否违法或违反规则,我对此还是很陌生。
解决方案
有什么理由不打印name
传递给函数的内容吗?
#define _GNU_SOURCE 1
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
//printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
printf("%s/%s\n", name, entry->d_name);
}
}
closedir(dir);
}
int main() {
system("mkdir -p dir/dir; touch dir/file dir/dir/file");
listdir(".", 0);
return 0;
}
可在onlinedbg获得实时代码。
推荐阅读
- java - Java - 为什么 Log4j2 将日志保存到 .gz 文件而不是 .txt
- acumatica - Acumatica/Acuminator 错误 PX1016 - 查找有关此 Acuminator 错误的更多信息
- flutter - 构建颤动后如何设置动画?
- javascript - 如何在javascript中将键和值推送到数组中
- google-sheets - 如何使用 Google Sheet API 知道哪些单元格或单元格范围在 Google 电子表格中受到保护?
- javascript - 使用没有任何插件的javascript从一个拖动到另一个
我有两个表:乘客和航班。
<table> <tr> <th> Passenger Name </th> <th> Passenger Flight </th> </tr> <tr> <td> Passenger 1 </td> <td> FR5412 &l
- android - FirebaseCrashlytics.getInstance() 在多进程应用程序中导致应用程序崩溃
- javascript - 如果单击选项卡,下拉菜单关闭
- javascript - 如何等待promise解决然后在reactjs中渲染
- python - 尽管匹配网络表单数据,但无法使用 requests.post 登录网站