|
> _findfirstは引数は2つしか指定できないのですが・・・
> すいません。初心者なんでその辺よく分からないんです。
_findfirst の引数が 2つだとわかっているのは、マニュアルを見ている
ということですよね。そこに使用例は載っていませんか。
とりあえず、私の書いたプログラムを説明なしに提示しますので、
どういう意味なのかを自分でよく調べてみてください。
#include <stdio.h>
#include <string.h>
#include <io.h>
void grep(const char *path, const char *str)
{
char buf[1024];
FILE *fp = fopen(path, "r");
if (fp == NULL) {
printf("can't open %s\n", path); return;
}
while (fgets(buf, sizeof buf, fp)) {
if (strchr(buf, '\n') == NULL) /* may be binary file */
return;
if (strstr(buf, str))
printf("%s: %s", path, buf);
}
fclose(fp);
}
void dir(char *path, int len, const char *str)
{
struct _finddata_t fd; long handle; int n;
strcpy(path + len, "\\*");
handle = _findfirst(path, &fd);
if (handle == -1) return;
do {
if (strcmp(fd.name, "..") == 0) continue;
if (strcmp(fd.name, ".") == 0) continue;
n = sprintf(path + len, "\\%s", fd.name);
if (fd.attrib & _A_SUBDIR)
dir(path, len + n, str);
else
grep(path, str);
} while (_findnext(handle, &fd) == 0);
_findclose(handle);
}
int main(int argc, char **argv)
{
char path[4096];
if (argc != 3) { printf("usage: %s string path", argv[0]); return 1; }
strcpy(path, argv[2]);
dir(path, strlen(path), argv[1]);
return 0;
}
|