ÇSTAT()忽略文件

编程入门 行业动态 更新时间:2024-10-13 10:25:12
本文介绍了ÇSTAT()忽略文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用C语言进行了统计功能的一个问题我的应用程序必须列出两个目录中的所有文件(第二目录尚未实现)。当dir1的被设置为。对于当前目录它会列出所有文件。如果我将其更改为所需目录只列出一个文件。

的#include<&stdio.h中GT;#包括LT&;&dirent.h GT;#包括LT&; SYS / stat.h>#包括LT&; SYS / types.h中>main()的{    DIR * DIR1;    DIR * DIR2;    方向1 =执行opendir(/家庭/嗵/文件/ UNI / Dropbox的/ OS / C / 1 /);    DIR2 =执行opendir(/家庭/嗵/文件/ UNI / Dropbox的/ OS / C / 2 /);    结构的dirent *耳鼻喉科;    结构统计FILESTAT;    如果(方向1!= NULL)    {        / *同时有目录中的文件读取* /        而((ENT = READDIR(DIR1))!= NULL)        {        / *的printf(在1 \\ n); < - 调试 - > * /        如果(STAT(ent-> d_name,&安培; FILESTAT)== 0)        {            / *忽略。和..和隐藏文件* /            / *的printf(2 \\ n); < - 调试 - > * /            如果(ent->!'。'd_name [0] =)            {                / *的printf(3 \\ n); < - 调试 - > * /                的printf(\\ n);                的printf(文件:%s \\ n,ent-> d_name);                的printf(文件大小:%d个\\ N,fileStat.st_size);                的printf(----------------------------------- \\ n);            }        }    }    / *关闭1号目录* /    closedir(方向1);    / *关闭第二目录* /    closedir(DIR2);    }    其他    {        / *打印错误如果该目录不能打开* /        PERROR();    }}

运行的程序的结果是如下:

汤姆@ x60deb:〜/文档/ UNI / Dropbox的/ OS / C $ ./ffffuuuuuu文件:ffffuuuuuu.c文件大小:1045-----------------------------------

这是它正在读取目录LS的结果是:

汤姆@ x60deb:〜/文档/ UNI / Dropbox的/ OS / C / 1 $ ls -l命令共有36-rw-R - R-- 1汤姆汤姆356 12月12日23:36 cwTest2.c-rw-R - R-- 1汤姆汤姆322 12月12日23:36 cwTest.c-rw-R - R-- 1汤姆汤姆627 12月12日23:36 ffffuuuuuu.c-rw-R - R-- 1汤姆汤姆12年12月6日23:32文件-rw-R - R-- 1汤姆汤姆12年12月6日23时32文件2-rw-R - R-- 1汤姆汤姆12年12月6日23:45 file2.file-rw-R - R-- 1汤姆汤姆12年12月15日23:33文件3-rw-R - R-- 1汤姆汤姆12年12月15日23:45 file3.file-rw-R - R-- 1汤姆汤姆12年12月6日23:45的File.File

在此先感谢,汤姆。

解决方案

的#include<&stdio.h中GT;#包括LT&;&string.h中GT;#包括LT&;&dirent.h GT;#包括LT&; SYS / stat.h>#包括LT&; SYS / types.h中>INT主要(无效){    字符*目录名称=/家/嗵/文件/ UNI / Dropbox的/ OS / C / 1 /;    DIR * DIR1;    焦炭路径[11111]    为size_t LEN;    结构的dirent *耳鼻喉科;    结构统计FILESTAT;    方向1 =执行opendir(目录名);    LEN = strlen的(目录名);    的memcpy(路径,目录名,LEN + 1);    结构的dirent *耳鼻喉科;    结构统计FILESTAT;    如果(方向1!= NULL)    {        / *同时有目录中的文件读取* /        而((ENT = READDIR(DIR1))!= NULL)        {        / *的printf(在1 \\ n); < - 调试 - > * /        的strcpy(路径+ LEN,ent-> d_name);        如果(STAT(路径和放大器; FILESTAT)== 0)        {            / *忽略。和..和隐藏文件* /            / *的printf(2 \\ n); < - 调试 - > * /            如果(ent->!'。'd_name [0] =)            {                / *的printf(3 \\ n); < - 调试 - > * /                的printf(\\ n);                的printf(文件:%s \\ n,ent-> d_name);                的printf(文件大小:%d个\\ N,fileStat.st_size);                的printf(----------------------------------- \\ n);            }        }    }    / *关闭1号目录* /    closedir(方向1);    }    其他    {        / *打印错误如果该目录不能打开* /        PERROR();    }返回0;}

更新,因为有些人看不懂,我会在这里我原来的评论:

什么是你的当前目录?这些条目是相对于/家/嗵/文件/ UNI / Dropbox的/ OS / C / 1 /(你应该给STAT()的完整路径)另:有超过条目。和..与。开头。

I'm having an issue with the stat function in C. My application must list all files in two directories (2nd directory not implemented yet). When dir1 is set to "." for current directory it lists all files. If I change it to the required directory it will only list one file.

#include <stdio.h> #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> main () { DIR *dir1; DIR *dir2; dir1 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/1/"); dir2 = opendir ("/home/tom/Documents/Uni/Dropbox/OS/C/2/"); struct dirent *ent; struct stat fileStat; if (dir1 != NULL) { /* while there are files to read in the directory */ while ((ent = readdir (dir1)) != NULL) { /*printf ("In 1\n"); <--debugging--> */ if (stat(ent->d_name,&fileStat) == 0) { /* ignores . and .. and hidden files */ /* printf ("In 2\n"); <--debugging--> */ if(ent->d_name[0] != '.') { /* printf ("In 3\n"); <--debugging--> */ printf ("\n"); printf ("File: %s\n", ent->d_name); printf ("File size: %d\n", fileStat.st_size); printf ("-----------------------------------\n"); } } } /* close the 1st directory */ closedir (dir1); /* close the 2nd directory */ closedir (dir2); } else { /* prints an error if the directory can not be opened */ perror (""); } }

The result of running the program is below:

tom@x60deb:~/Documents/Uni/Dropbox/OS/C$ ./ffffuuuuuu File: ffffuuuuuu.c File size: 1045 -----------------------------------

This is the result of ls in the directory it is reading:

tom@x60deb:~/Documents/Uni/Dropbox/OS/C/1$ ls -l total 36 -rw-r--r-- 1 tom tom 356 Dec 12 23:36 cwTest2.c -rw-r--r-- 1 tom tom 322 Dec 12 23:36 cwTest.c -rw-r--r-- 1 tom tom 627 Dec 12 23:36 ffffuuuuuu.c -rw-r--r-- 1 tom tom 6 Dec 12 23:32 file -rw-r--r-- 1 tom tom 6 Dec 12 23:32 file2 -rw-r--r-- 1 tom tom 6 Dec 12 23:45 file2.file -rw-r--r-- 1 tom tom 15 Dec 12 23:33 file3 -rw-r--r-- 1 tom tom 15 Dec 12 23:45 file3.file -rw-r--r-- 1 tom tom 6 Dec 12 23:45 file.file

Thanks in advance, Tom.

解决方案

#include <stdio.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> int main (void) { char * dirname = "/home/tom/Documents/Uni/Dropbox/OS/C/1/" ; DIR *dir1; char path[11111]; size_t len; struct dirent *ent; struct stat fileStat; dir1 = opendir (dirname); len = strlen ( dirname); memcpy(path, dirname, len+1); struct dirent *ent; struct stat fileStat; if (dir1 != NULL) { /* while there are files to read in the directory */ while ((ent = readdir (dir1)) != NULL) { /*printf ("In 1\n"); <--debugging--> */ strcpy(path+len, ent->d_name); if (stat( path,&fileStat) == 0) { /* ignores . and .. and hidden files */ /* printf ("In 2\n"); <--debugging--> */ if(ent->d_name[0] != '.') { /* printf ("In 3\n"); <--debugging--> */ printf ("\n"); printf ("File: %s\n", ent->d_name); printf ("File size: %d\n", fileStat.st_size); printf ("-----------------------------------\n"); } } } /* close the 1st directory */ closedir (dir1); } else { /* prints an error if the directory can not be opened */ perror (""); } return 0; }

UPDATE because some people cannot read, I'll add my original comment here:

what is your current directory? the entries are relative to "/home/tom/Documents/Uni/Dropbox/OS/C/1/" (you should give stat() the full pathname) ALSO: there are more entries than "." and ".." that start with ".".

更多推荐

ÇSTAT()忽略文件

本文发布于:2023-10-08 14:52:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   STAT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!