C 遍历服务器文件夹中的文件,windows下使用C/C++怎么遍历目录并读取目录下的文件列表?...

Windows 下最本质的做法,是直接调用 Windows API,解决你的这个问题,需要三个 API 函数:

应该基本上可以顾名思义了吧。

核心用法如下:

HANDLE hFind = FindFirstFile(szDir, &ffd);

// List all the files in the directory with some info about them.

do

{

if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

{

_tprintf(TEXT(" %s

}

else

{

filesize.LowPart = ffd.nFileSizeLow;

filesize.HighPart = ffd.nFileSizeHigh;

_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart);

}

}

while (FindNextFile(hFind, &ffd) != 0);

FindClose(hFind);

优势在于不需要任何第三方库,可以获得其他更多文件的信息,譬如想知道文件的修改时间之类的,也可以在这上面扩展。

唯一的劣势,应该就是无法直接跨平台,但可以通过一式两份的方式跨,要更加高效。

如果想直接跨平台,即 Linux 与 Windows 下使用同一份代码,还可以考虑使用 Qt 框架。QDir可以以最清晰简单的代码解决上述问题:

// lists all the files in the current directory

#include

#include

int main(int argc, char *argv[])

{

QCoreApplication app(argc, argv);

QDir dir;

dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);

dir.setSorting(QDir::Size | QDir::Reversed);

QFileInfoList list = dir.entryInfoList();

std::cout << " Bytes Filename" << std::endl;

for (int i = 0; i < list.size(); ++i) {

QFileInfo fileInfo = list.at(i);

std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.fileName()));

std::cout << std::endl;

}

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用<Windows.h>实现遍历路径文件夹所有文件夹文件复制到指定路径的示例代码: ```c #include <Windows.h> #include <stdio.h> #include <string.h> void copyDirectory(const char* srcPath, const char* destPath); int main() { const char* srcPath = "C:\\Path\\To\\Source\\Folder"; const char* destPath = "C:\\Path\\To\\Destination\\Folder"; copyDirectory(srcPath, destPath); return 0; } void copyDirectory(const char* srcPath, const char* destPath) { char src[MAX_PATH], dest[MAX_PATH]; WIN32_FIND_DATA findData; HANDLE hFind; // Create the destination directory if it doesn't exist CreateDirectory(destPath, NULL); // Append wildcard to the source path strcpy(src, srcPath); strcat(src, "\\*"); // Find the first file in the source directory hFind = FindFirstFile(src, &findData); if (hFind == INVALID_HANDLE_VALUE) { printf("Error: Invalid handle value.\n"); return; } do { // Skip . and .. directories if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0) continue; // Build the full path for the source and destination files sprintf(src, "%s\\%s", srcPath, findData.cFileName); sprintf(dest, "%s\\%s", destPath, findData.cFileName); // If the file is a directory, recursively copy the directory if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) copyDirectory(src, dest); else // Otherwise, copy the file CopyFile(src, dest, FALSE); } while (FindNextFile(hFind, &findData)); // Close the handle and return FindClose(hFind); } ``` 该代码通过递归遍历目录的所有文件夹文件,并将它们复制到目标目录。其使用了Win32 API的一些函数,如FindFirstFile、CreateDirectory和CopyFile等。需要注意的是,如果复制的文件已经存在于目标目录,则会覆盖原有文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值