[英]C code not displaying output
因此,我正在使用以UNIX为终端的C程序,该程序应该递归调用我的目录。 输出应该给我目录本身以及它们被修改的时间段。
该代码有效,可以很好地编译,但是拒绝显示其输出。 现在,我只需要显示输出即可。 但这就是它给我的...我尝试了./compdir、./compdir dir1 dir2以及./compdir dir2 dir1 ...在UNIX终端中唯一可行的解决方案,但无济于事。
有人可以帮我吗? 这是下面的代码。 我感谢您的帮助。
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#define E_INVALIDSWITCH 100
#define E_TOOMANYPARAMS 101
#define E_TOOFEWPARAMS 102
#define E_OPENDIRFAIL 103
void status();
void greetDirectory(char * baseDir, char * modDir);
int main(int argc, char ** argv)
{
int R_switch = 0;
char * modDir = NULL;
char * baseDir = NULL;
int i;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (strcmp(argv[i], "-R") == 0)
{
R_switch = 1;
}
else
{
fprintf(stderr, "Error: Invalid switch %s\n", argv[i]);
status();
return E_INVALIDSWITCH;
}
}
else
{
if (baseDir == NULL)
{
baseDir = argv[i];
}
else if (modDir == NULL)
{
modDir = argv[i];
}
else
{
fprintf(stderr, "Error: Too many parameters\n");
status();
return E_TOOMANYPARAMS;
}
}
}
if (modDir == NULL)
{
fprintf(stderr, "Error: Too few parameters\n");
status();
return E_TOOFEWPARAMS;
}
if (R_switch)
{
greetDirectory(baseDir, modDir);
}
return 0;
}
void status()
{
printf("\t-R\tGreet all files in current directory\n");
printf("\t<modDir>\tName of person being greeted\n");
}
char *formatdate(char *buff, time_t val)
{
strftime(buff,200, "%d.%m.%Y %H:%M:%S", localtime(&val));
return buff;
}
void greetDirectory(char * baseDir, char * modDir)
{
DIR * directory;
struct dirent * dirInfo;
directory = opendir(baseDir);
if (directory == NULL)
{
fprintf(stderr,
"opendir() failed for '%s', errno=%d\n",
baseDir,
errno);
exit(E_OPENDIRFAIL);
}
while((dirInfo = readdir(directory)) != NULL)
{
if (strcmp(dirInfo->d_name, ".") != 0 &&
strcmp(dirInfo->d_name, "..") != 0)
{
/* BASE DIR*/
char basePathName[PATH_MAX];
strcpy(basePathName, baseDir);
strcat(basePathName, "/");
strcat(basePathName, dirInfo->d_name);
/* MOD DIR */
char modPathName[PATH_MAX];
strcpy(modPathName, modDir);
strcat(modPathName, "/");
strcat(modPathName, dirInfo->d_name);
struct stat statBuf;
struct stat otherStatBuf;
if (stat(basePathName, &statBuf) == -1)
{
/* error! */
fprintf(stderr, "stat() failed for '%s'", basePathName);
}
else
{
//display time
stat(modPathName, &otherStatBuf);
if (statBuf.st_mtime > otherStatBuf.st_mtime)
{
char date[36];
printf("Modify: %s\n", formatdate(date, otherStatBuf.st_mtime));
}
if (S_ISDIR(statBuf.st_mode))
{
/* if this is a directory, then recurse! */
greetDirectory(basePathName, modPathName);
}
else
{
/* else if it's a file, then greet it! */
printf("entry name = %s\n", modPathName);
}
}
}
}
closedir(directory);
}
仅当您指定-R
开关时,您的代码才会提供输出。
如果您不希望出现这种情况,则需要删除第72行的if块并直接调用greetDirectory
函数。
// Remove if block
if (R_switch)
{
greetDirectory(baseDir, modDir);
}
// Replace with
greetDirectory(baseDir, modDir);
这是完整的代码:
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#define E_INVALIDSWITCH 100
#define E_TOOMANYPARAMS 101
#define E_TOOFEWPARAMS 102
#define E_OPENDIRFAIL 103
#define MAX_DIR_NAME_SIZE 100
void status();
void greetDirectory(char * baseDir, char * modDir);
int main(int argc, char ** argv)
{
int R_switch = 0;
char modDir[MAX_DIR_NAME_SIZE] = {0};
char baseDir[MAX_DIR_NAME_SIZE] = {0};
int i;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (strcmp(argv[i], "-R") == 0)
{
R_switch = 1;
continue;
}
else
{
fprintf(stderr, "Error: Invalid switch %s\n", argv[i]);
status();
return E_INVALIDSWITCH;
}
}
else
{
if (baseDir[0] == 0 && modDir[0] == 0)
{
strcpy(baseDir, argv[i]);
strcpy(modDir, argv[i]);
}
else
{
fprintf(stderr, "Error: Too many parameters\n");
status();
return E_TOOMANYPARAMS;
}
}
}
if (modDir[0] == 0)
{
fprintf(stderr, "Error: Too few parameters\n");
status();
return E_TOOFEWPARAMS;
}
if (R_switch)
{
greetDirectory(baseDir, modDir);
}
return 0;
}
void status()
{
printf("\t-R\tGreet all files in current directory\n");
printf("\t<modDir>\tName of person being greeted\n");
}
char *formatdate(char *buff, time_t val)
{
strftime(buff,200, "%d.%m.%Y %H:%M:%S", localtime(&val));
return buff;
}
void greetDirectory(char * baseDir, char * modDir)
{
DIR * directory;
struct dirent * dirInfo;
directory = opendir(baseDir);
if (directory == NULL)
{
fprintf(stderr,
"opendir() failed for '%s', errno=%d\n",
baseDir,
errno);
exit(E_OPENDIRFAIL);
}
while((dirInfo = readdir(directory)) != NULL)
{
if (strcmp(dirInfo->d_name, ".") != 0 &&
strcmp(dirInfo->d_name, "..") != 0)
{
/* BASE DIR*/
char basePathName[PATH_MAX];
strcpy(basePathName, baseDir);
strcat(basePathName, "/");
strcat(basePathName, dirInfo->d_name);
/* MOD DIR */
char modPathName[PATH_MAX];
strcpy(modPathName, modDir);
strcat(modPathName, "/");
strcat(modPathName, dirInfo->d_name);
struct stat statBuf;
struct stat otherStatBuf;
if (stat(basePathName, &statBuf) == -1)
{
/* error! */
fprintf(stderr, "stat() failed for '%s'", basePathName);
}
else
{
//display time
stat(modPathName, &otherStatBuf);
if (statBuf.st_mtime > otherStatBuf.st_mtime)
{
char date[36];
printf("Modify: %s\n", formatdate(date, otherStatBuf.st_mtime));
}
if (S_ISDIR(statBuf.st_mode))
{
/* if this is a directory, then recurse! */
greetDirectory(basePathName, modPathName);
}
else
{
/* else if it's a file, then greet it! */
printf("entry name = %s\n", modPathName);
}
}
}
}
closedir(directory);
}
主要问题是在循环中,您在其中将值分配给modDir
和baseDir
。 例如,假设您有参数-R
,那么您要打印出目录名称。 由于argv[1]
是-R
所以您将R_switch
的值指定为1
。 问题是, 您还要继续循环来分配baseDir
和modDir
-R
! 当argv[2]
是您要输入的任何目录时,应该在循环的下一次迭代中完成此操作。
解决此问题的最简单方法是添加一个continue;
将R_switch
的值设置为1之后的语句。
还要注意,在玩游戏时,我确实创建了具有MAX_DIR_NAME_SIZE
的限制,而MAX_DIR_NAME_SIZE
使用的char *
并不需要它。 将其改回应该很容易。 我还最终将modDir
和baseDir
的比较更改为if (baseDir[0] == 0 && modDir[0] == 0)
,应该通过将char *
与NULL
比较来完成(像这样)原来是)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.