|

楼主 |
发表于 2023-7-24 21:19:51
|
显示全部楼层
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include "dirent.h"
- int main()
- {
- // SetConsoleOutputCP(CP_UTF8);
-
- char path[256] = {0};
- int fileNum = 0;
- wchar_t wpath[256] = { 0 };
- printf("Enter path to list files: ");
- scanf("%s", path);
- printf("[%s]\n", path);
-
-
-
- DIR* pDir = NULL;
- dirent* pent = NULL;
- pDir = opendir(path);
-
- if (pDir == NULL) //
- {
- printf("打不开文件夹\n");
- system("pause");
- return -1;
- }
- while (1)
- {
- pent = readdir(pDir);
- char tmpname[256] = { 0 };
- if (pent != NULL)
- {
- //将wchar_t 转为 cahr
- // wcstombs(tmpname, pent->d_name, 256);
- if (pent->d_type == DT_BLK)
- {
-
- printf("DT_BLK:%s\n", tmpname);
- }
- else if (pent->d_type == DT_CHR)
- {
-
- printf("DT_CHR:[%s]\n", tmpname);
- }
- else if (pent->d_type == DT_DIR)
- {
- printf("DT_DIR:[%s]\n", tmpname);
- }
- else if (pent->d_type == DT_FIFO)
- {
- printf("DT_FIFO:[%s]\n", tmpname);
- }
- else if (pent->d_type == DT_LNK)
- {
- printf("DT_LNK:[%s]\n", tmpname);
- }
- else if (pent->d_type == DT_REG)
- {
- printf("DT_REG:[%s]\n", tmpname);
- }
- else if (pent->d_type == DT_SOCK)
- {
- printf("DT_UNKNOWN:[%s]\n", tmpname);
- }
- else
- {
- printf("********:[%s]\n", tmpname);
- }
- fileNum++;
- }
- else
- {
- break;
- }
- }
- closedir(pDir);
- printf("FileNum:%d\n", fileNum);
-
-
- system("pause");
- return 0;
- }
复制代码
|
|