【发布时间】:2018-10-08 18:18:13
【问题描述】:
我在 C 方面的经验并不丰富,但在我的一门课程中有一个作业,我在打开目录时遇到了一些问题。
该计划的目的是(意译):
- 如果没有提供标志或目录,则列出当前目录中的文件名。
- 如果提供了标志 (-l),请列出有关每个文件的更多信息。
- 如果提供了标志和目录,请转到该目录并列出有关该目录中每个文件(或目录)的信息。不必通过子目录进行递归。
它不需要处理只得到一个目录但没有标志,或者一个无效的目录。这是更多的概念验证。
我已经设法满足第 1 部分和第 2 部分,但第 3 部分不断中断,出现分段错误。
我的代码如下:
void recursedirect(char* flag, char* directory)
{
DIR* dir;
struct dirent *entry;
printf("Flag: %s\nDirectory: %s\n\n", flag, directory);
if (flag == NULL)
// No flag provided, therefore only do file names.
{
dir = opendir(".");
entry = readdir(dir);
while (entry != NULL)
{
printf ("%s\n", entry->d_name);
entry = readdir(dir);
}
}
else if (strcmp(flag, "-l") == 0 && directory == NULL)
// No directory provided, but flag provided
{
dir = opendir(".");
entry = readdir(dir);
while (entry != NULL)
{
fileinfo(directory);
entry = readdir(dir);
}
}
else if (strcmp(flag, "-l") != 0)
{
printf("Invalid flag.\n");
}
else
// A directory is provided
{
dir = opendir(directory);
if (dir != NULL)
{
entry = readdir(dir);
while (entry != NULL)
{
fileinfo(directory);
entry = readdir(dir);
}
}
else
printf("Something went wrong. It might be an invalid filename.\n");
}
}
具体来说,最后的 else 语句给我带来了麻烦。我试过给它我能想到的任何文件路径,硬编码和其他,它仍然给我带来问题。我猜这很简单,但我没有足够的经验知道它是什么。
fileinfo 是一个函数,它打印出以DIR 和dirent 作为参数的文件信息。但是,我能说的最好的错误绝对不是该代码中的错误。
编辑:我犯了不分享所有内容的错误。我的主要方法很简单。
int main (int argc, char** argv)
{
char* flag = argv[1];
char* directory = argv[2];
recursedirect(flag, directory);
}//main
它包括以下内容:
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
fileinfo 函数如下(稍作编辑,因为我意识到我不再需要给它一个 DIR *dir 的参数):
void fileinfo(struct dirent *entry)
{
struct stat fileStats;
stat(entry->d_name, &fileStats);
printf("File Name: %s\n", entry->d_name);
printf("File Size: %d bytes\n", (int) fileStats.st_size);
printf("Number of Blocks: %d\n", (int) fileStats.st_blocks);
printf("Number of Links/References: %d\n", (int) fileStats.st_nlink);
printf("I-Node: %d\n", (int) fileStats.st_ino);
printf("Device ID: %d\n", (int) fileStats.st_dev);
printf("User ID: %d\n", (int) fileStats.st_uid);
printf("Group ID: %d\n", (int) fileStats.st_gid);
stat("alphabet",&fileStats);
printf("Time of Last Access: %s", ctime(&fileStats.st_atime));
printf("Time of Last Modification: %s", ctime(&fileStats.st_mtime));
printf("Time of Last Status Change: %s", ctime(&fileStats.st_ctime));
stat(entry->d_name, &fileStats);
printf("File Permissions: ");
printf((int) (S_ISDIR(fileStats.st_mode)) ? "d" : "-");
printf((int) (fileStats.st_mode & S_IRUSR) ? "r" : "-");
printf((int) (fileStats.st_mode & S_IWUSR) ? "w" : "-");
printf((int) (fileStats.st_mode & S_IXUSR) ? "x" : "-");
printf((int) (fileStats.st_mode & S_IRGRP) ? "r" : "-");
printf((int) (fileStats.st_mode & S_IWGRP) ? "w" : "-");
printf((int) (fileStats.st_mode & S_IXGRP) ? "x" : "-");
printf((int) (fileStats.st_mode & S_IROTH) ? "r" : "-");
printf((int) (fileStats.st_mode & S_IWOTH) ? "w" : "-");
printf((int) (fileStats.st_mode & S_IXOTH) ? "x\n\n" : "-\n\n");
}
关于失败输入的更多说明,./files 和 ./files -l 都可以。带有任何无效标志的./files 按预期捕获。但是,./files -l *any valid filepath* 始终失败。
一切都是在 c9.io IDE 的 Ubuntu 14.04.5 中使用 gcc 编译的。
谢谢!
编辑 2:我尝试删除并放回文件,但现在奇怪的是我没有收到错误。但是,现在它不会打印任何内容的完整文件信息。我会再排查一些问题。
编辑 3:我已修复它,但有一个例外。问题是检查标志的 if 语句不正确。显然 IDE 出了点问题。我现在收到此错误:Couldn't open MANPATH=/home/ubuntu/.nvm/versions/node/v6.11.2/share/man:/usr/local/rvm/rubies/ruby-2.4.0/share/man:/usr/local/man:/usr/local/share/man:/usr/share/man:/usr/local/rvm/man
看起来这不是代码,并且在某种程度上是 c9 服务器的问题。我尝试为源代码和编译代码文件提供完全权限,重新启动工作区,并重新启动 Apache2。谢谢大家的一切!
【问题讨论】:
-
(将代码缩进四个空格。如帮助弹出窗口中所示,有一个按钮。)
-
@usr2564301:不,你明白了——不用担心! :)
-
旁白:我希望每个成功的
opendir()都有一个closedir()。我没有看到。 -
“它仍然给我带来问题。” --> 有什么问题?
fileinfo(directory);代码是可疑的。为什么会循环呢?应该是fileinfo(dir);或fileinfo(entry)还是fileinfo(entry->d_name); -
"fileinfo 是一个函数,它打印出给定 DIR 和 dirent 作为参数的文件信息。" 是对人的描述。发布它的函数签名会更好。