[3H] 请问有关Linux下的几个 C 函数(188分)

3

3h

Unregistered / Unconfirmed
GUEST, unregistred user!
要在LINUX写一小小程序,要找出符合要求的所有文件,例如 *.*,就几行,
在DOS/Windows下没有问题。
不过用到了FindFirst函数,<dir.h>
但LINUX下没这个头。
可能它不是用findfirst/findnext?用了其它?
请指出详细的查找所有文件所用的函数,及例子。
谢谢!
 
#include <dirent.h>
opendir
 
好像delphi中也有类似的函可以相互参考吧
linuc下的c 没有用过
 
谢谢,能不能说说OPENDIR的具体用法呢?
 
#include <dirent.h>
DIR* dir=opendir(dirname);
struct dirent* ent;
while ((ent = readdir(dir)) != NULL)
printf("%s/n",ent->d_name);
//输出子目录名字和文件名
closedir(dir);
 
unix下面是支持的,linux不知道是否支持
 
#include <dirent.h>
DIR* dir=opendir("/local/");
struct dirent* ent;
while ((ent = readdir(dir)) != NULL)
printf("%s/n",ent->d_name);
closedir(dir);
================================================
bash-2.05# gcc c.c
c.c:2: initializer element is not constant
c.c:4: parse error before `while'
c.c:6: warning: parameter names (without types) in function declaration
c.c:6: warning: data definition has no type or storage class
 
PIPI老兄:
我用的是Linux7.2的gcc。看来不行。
Windows咱还能凑合,有帮助,这个linux下的真不知道怎么办才好,
哥们可有好点的编辑器,最好有语法帮助的,呵呵
 
你这个是c,不是c++啊,那这样:
#include <dirent.h>
DIR* dir;
struct dirent* ent;
dir=opendir("/local/");
while ((ent = readdir(dir)) != NULL)
printf("%s/n",ent->d_name);
closedir(dir);
 
good.编译成功!老凶你忘记写mail了。:)
随便说,struct dirent的结构是怎么样的?
有没有相干的文档学习学习?
 
email不提供,另外我一点也不凶,不要叫我老凶
dirent 你自己打开 <dirent.h> 就看见了
另外好快点给我分了:)
 
写错了,是main()
那么猴急干嘛,一定给你分的啦。
 
我正等米下锅呢
 
#include <stdio.h>
#include <dirent.h>
#include <fcntl.h>
void main()
{
DIR* dir;
struct dirent* ent;
char* BUF[4096];
int Hand, Hand2, n;
chdir("/local");
Hand = open("/list.txt", O_RDWR);
if (Hand > 0)
{
dir=opendir("/local");
while ((ent = readdir(dir)) != NULL)
{
Hand2 = open(ent->d_name, O_RDONLY);
n=read(Hand2, BUF, 4096);
if (n>0)
{
write(Hand, BUF, n);
// printf("%s/n",ent->d_name);
}
close(Hand2);
}
closedir(dir);
}
close(Hand);
}
读出所有/local下的文件的内容(都是小文本)并写入/list.txt中。
 
顶部