当前位置: 技术问答>linux和unix
求助!linux下的c语言高手请进
来源: 互联网 发布时间:2015-01-14
本文导语: 小弟现遇一问题:我想顺序读出一个目录下的所有文件,该怎样写代码?由于我不知道该目录下文件的文件名,有什么办法么?请高手指教,谢谢各位啦!! | man 3 scandir man 3 alphasort 自己...
小弟现遇一问题:我想顺序读出一个目录下的所有文件,该怎样写代码?由于我不知道该目录下文件的文件名,有什么办法么?请高手指教,谢谢各位啦!!
|
man 3 scandir
man 3 alphasort
自己去查吧 今天我太累了
要是要例子告诉我 晚上给你写个
man 3 alphasort
自己去查吧 今天我太累了
要是要例子告诉我 晚上给你写个
|
#include
#include
#include
void scandir(char *dirname)
{
DIR *dir;
struct dirent *ent;
printf("First pass on '%s':n",dirname);
if ((dir = opendir(dirname)) == NULL)
{
perror("Unable to open directory");
exit(1);
}
while ((ent = readdir(dir)) != NULL)
printf("%sn",ent->d_name);
printf("Second pass on '%s':n",dirname);
rewinddir(dir);
while ((ent = readdir(dir)) != NULL)
printf("%sn",ent->d_name);
if (closedir(dir) != 0)
perror("Unable to close directory");
}
void main(int argc,char *argv[])
{
if (argc != 2)
{
printf("usage: opendir dirnamen");
exit(1);
}
scandir(argv[1]);
exit(0);
}
#include
#include
void scandir(char *dirname)
{
DIR *dir;
struct dirent *ent;
printf("First pass on '%s':n",dirname);
if ((dir = opendir(dirname)) == NULL)
{
perror("Unable to open directory");
exit(1);
}
while ((ent = readdir(dir)) != NULL)
printf("%sn",ent->d_name);
printf("Second pass on '%s':n",dirname);
rewinddir(dir);
while ((ent = readdir(dir)) != NULL)
printf("%sn",ent->d_name);
if (closedir(dir) != 0)
perror("Unable to close directory");
}
void main(int argc,char *argv[])
{
if (argc != 2)
{
printf("usage: opendir dirnamen");
exit(1);
}
scandir(argv[1]);
exit(0);
}
|
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL){
fprintf(stderr,"cannot open directory: %sn",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/* found a dirent, but ignore , and .. */
if(strcmp(".",entry->d_name)==0||
strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/n",depth,"",entry->d_name);
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4);
}
else
printf("%*s%sn",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char * argv[])
{
char *topdir=".";
if(argc>=2)
topdir=argv[1];
printf("Directory scan of %s n",topdir);
printdir(topdir,0);
printf("done. n");
exit(0);
}
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(dir))==NULL){
fprintf(stderr,"cannot open directory: %sn",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
/* found a dirent, but ignore , and .. */
if(strcmp(".",entry->d_name)==0||
strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/n",depth,"",entry->d_name);
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4);
}
else
printf("%*s%sn",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char * argv[])
{
char *topdir=".";
if(argc>=2)
topdir=argv[1];
printf("Directory scan of %s n",topdir);
printdir(topdir,0);
printf("done. n");
exit(0);
}
|
在这方面还不如直接用shell。
|
不知道你的顺序是正序还是反序,
用系统调用最好,简单可靠!!
如system,popen,...
详man一下!
用系统调用最好,简单可靠!!
如system,popen,...
详man一下!
|
popen("ls -1 *", "r");
足够了。
足够了。
|
建议你用链表结构。