[英]Creating a grep command from scratch
您好,我一直在Internet上混合和匹配代碼,以嘗試創建一個遞歸搜索目錄的命令。 我現在很困。 目前,我的目錄中只有兩個文件(沒有子目錄),並且打印輸出不斷顯示我的文件比實際更多。
這是我的打印輸出:
entered
entered
entered
enteredIF
entered
enteredIF
entered
enteredIF
entered
enteredIF
我認為應該做的只是兩次打印enterIF。 有趣的是,我的兩個文件中有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);
}
將輸入的printf更改為: printf("saw filename '%s'\\n", entry->d_name);
然后您可以看到它認為正在讀取的文件名。 也許您的點文件不止於此.
和..
–約翰·H
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.