本帖最后由 523066680 于 2015-11-22 23:45 编辑
最近重新学C,
没有测试链接的目录,
风格好渣……- #include <cstdio>
- #include <cstdlib>
- #include <cwchar>
- #include <cstring>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <locale.h>
-
- #define NAME_MAX 1024
-
- void func(wchar_t path[]);
- static long long int TotalSize = 0;
- static long long int FileCount = 0;
- static long long int DirCount = 0;
-
- int main(int argc, char *argv[] )
- {
- setlocale( LC_ALL, ".936" );
- wchar_t wspath[1024] = L"";
- char ts_str[30] = "";
-
- if (argc > 1)
- {
- mbstowcs( wspath, argv[1], strlen(argv[1]) );
-
- _WDIR * a;
- DIR * b;
-
- if ( (a = _wopendir(wspath)) == NULL )
- {
- fprintf(stderr, "Argument is not correct!");
- }
- else
- {
- func( wspath );
- fprintf(stderr, "%20lld Folders\n", DirCount);
- fprintf(stderr, "%20lld Files\n" , FileCount);
- fprintf(stderr, "%20lld Bytes\n" , TotalSize);
- fprintf(stderr, "%20lld MB\n" , TotalSize/1024/1024);
- fprintf(stderr, "%20lld GB" , TotalSize/1024/1024/1024);
- }
- }
- else
- {
- fprintf(stderr, "No arguments!");
- }
-
- return 0;
- }
-
- void func(wchar_t path[])
- {
- _WDIR * a = _wopendir(path);
- _wdirent * dp;
- _WDIR * aa;
- struct _stati64 stbuf;
- wchar_t fullpath[NAME_MAX] = L"";
-
- while (dp = _wreaddir(a))
- {
- if (
- wcscmp(dp->d_name, L".") == 0
- || wcscmp(dp->d_name, L"..") == 0
- )
- {
- continue;
- }
-
- swprintf(fullpath, L"%ls\\%ls", path, dp->d_name);
- _wstati64(fullpath, &stbuf);
-
- if ( (stbuf.st_mode & S_IFMT) == S_IFDIR )
- {
- DirCount ++;
- func( fullpath );
- }
- else
- {
- TotalSize += (long long int)stbuf.st_size;
- FileCount ++;
- }
-
- }
- _wclosedir(a);
- }
复制代码 1962 Folders
17329 Files
76636660679 Bytes
73086 MB
71 GB |