No. - 给我们所有人的教训:即使在琐事中也有陷阱。 --Eric SosmanNo.--"A lesson for us all: Even in trivia there are traps."--Eric Sosman 当然!您可以计算每个文件的大小,然后累积它们! 引用来自busybox: long du(char * filename) { struct stat statbuf; long sum; if((lstat(filename,&) ; statbuf))!= 0){ perror_msg("%s",filename); 返回0; } if(du_depth == 0) dir_dev = statbuf.st_dev; else if(one_file_system&& dir_dev!= statbuf.st_dev) 返回0; du_depth ++; sum =(statbuf.st_blocks>> 1); / *不要添加符号链接指向的东西* / if(S_ISLNK(statbuf.st_mode)){ sum = 0L; if(du_depth == 1){ } } if(S_ISDIR( statbuf.st_mode)){ DIR * dir; struct dirent * entry; char * newfile; dir = opendir(filename); if(!dir){ du_dep th - ; 返回0; } newfile = last_char_is(filename,''/''); if(newfile) * newfile =''\ 0''; while((entry = readdir(dir) ))){ char * name = entry-> d_name; if((strcmp(name," ..")== 0) || (strcmp(name,"。")== 0)){ 继续; } newfile = concat_path_file(filename,姓名); 总和+ = du(新文件); 免费(新文件); } closedir (dir); print(sum,filename); } else if(statbuf.st_nlink> 1&&! count_hardlinks){ / *只添加一次硬链接的文件* / if(is_in_ino_dev_hashtable(& statbuf,NULL)){ sum = 0L; if(du_depth == 1) print(sum,filename); } else { add_to_ino_dev_hashtable(& statbuf,NULL); } } du_depth--; 返还金额; }Sure! You can calculate the size of each file, then accumulate them!Quote from busybox:long du(char *filename){struct stat statbuf;long sum;if ((lstat(filename, &statbuf)) != 0) {perror_msg("%s", filename);return 0;}if (du_depth == 0)dir_dev = statbuf.st_dev;else if (one_file_system && dir_dev != statbuf.st_dev)return 0;du_depth++;sum = (statbuf.st_blocks >> 1);/* Don''t add in stuff pointed to by symbolic links */if (S_ISLNK(statbuf.st_mode)) {sum = 0L;if (du_depth == 1) {}}if (S_ISDIR(statbuf.st_mode)) {DIR *dir;struct dirent *entry;char *newfile;dir = opendir(filename);if (!dir) {du_depth--;return 0;}newfile = last_char_is(filename, ''/'');if (newfile)*newfile = ''\0'';while ((entry = readdir(dir))) {char *name = entry->d_name;if ((strcmp(name, "..") == 0)|| (strcmp(name, ".") == 0)) {continue;}newfile = concat_path_file(filename, name);sum += du(newfile);free(newfile);}closedir(dir);print(sum, filename);}else if (statbuf.st_nlink > 1 && !count_hardlinks) {/* Add files with hard links only once */if (is_in_ino_dev_hashtable(&statbuf, NULL)) {sum = 0L;if (du_depth == 1)print(sum, filename);}else {add_to_ino_dev_hashtable(&statbuf, NULL);}}du_depth--;return sum;} 这不是C. 长款; if((lstat(filename, & statbuf))!= 0){ perror_msg("%s",filename);This is not C. long sum; if ((lstat(filename, &statbuf)) != 0) { perror_msg("%s", filename); 这些都不是。 你把C与POSIX或其他与Unix相关的标准混淆了。 OP没有声明他们是在一个实现POSIX标准的系统上。 到OP:ANSI C没有目录的概念,并且不,没有 直接获取文件大小的方法。你可以打开文件,并计算字符数,但你可能正在寻找一些特定于系统的实现,比如上一篇文章中提到的那些。你需要在一个谈论你的平台的小组中提出这些问题,因为你最有可能在那里得到最好的答案。 Martien - | Martien Verbruggen | Trading Post Australia |戴夫在这里,根密码是什么? |And these aren''t either.You''re confusing C with POSIX, or one of the other Unix-relatedstandards. The OP did not state they were on a system that implementsthe POSIX standard.To the OP: ANSI C has no concept of a directory, and no, there is nodirect way to get the size of a file. You can open the file, and countthe characters, but you''re probably looking for some system specificimplementations, like the ones mentioned in the previous post. Youshould ask these questions in a group that talks about your platform,because you are most likely to get the best answer there.Martien--|Martien Verbruggen |Trading Post Australia | Hi, Dave here, what''s the root password?| 这篇关于计算目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 22:45