您好我一直在互联网上混合和匹配代码,试图制作一个递归搜索目录的命令。我现在很困难。目前我的目录中只有两个文件(没有子目录),我的打印输出一直显示我有比实际更多的文件。
这是我的打印输出:
entered
entered
entered
enteredIF
entered
enteredIF
entered
enteredIF
entered
enteredIF
我认为它应该做的只是打印进入IF两次。有趣的是,我的两个文件中有11个字符,并且有11个打印语句。也许这与我的问题有某种关系。
有谁能告诉我如何获得两个打印输出(每个文件一个)?
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <dirent.h>
/* A process is a single process. */
typedef struct process
{
struct process *next; /* next process in pipeline */
char **argv; /* for exec */
pid_t pid; /* process ID */
char completed; /* true if process has completed */
char stopped; /* true if process has stopped */
int status; /* reported status value */
} process;
/* A job is a pipeline of processes. */
typedef struct job
{
struct job *next; /* next active job */
char *command; /* command line, used for messages */
process *first_process; /* list of processes in this job */
pid_t pgid; /* process group ID */
char notified; /* true if user told about stopped job */
struct termios tmodes; /* saved terminal modes */
int stdin, stdout, stderr; /* standard i/o channels */
} job;
/* The active jobs are linked into a list. This is its head. */
job *first_job = NULL;
int main(int argc, char** argv) {
// char cwd[1024]; // buffer
// char* sdirectory = getcwd(cwd, sizeof(cwd));
// printf("dir name: %s\n", sdirectory);
int file_count = 0;
DIR* dirp;
struct dirent* entry;
dirp = opendir(".");
while ((entry = readdir(dirp)) != NULL) {
printf("entered\n");
if (entry-> d_type == DT_REG) {
printf("enteredIF\n");
file_count++;
}
}
closedir(dirp);
printf("file count: %d\n", file_count);
}
答案 0 :(得分:0)
将输入的printf更改为:printf("saw filename '%s'\n", entry->d_name);
,然后您就可以看到它认为正在阅读的文件名。也许你有更多的点文件而不仅仅是.
和..
。 - JohnH