Windows系统C语言获取文件夹来的所有文件名的方法

Windows系统C语言获取文件夹来的所有文件名的方法,代码如下:

#include <io.h>
#include <stdio.h>
#include <direct.h>
 
#define MAX_LEN 4096
 
int main(void)
{
    char root[] = "E:\\Workspace";
    struct _finddata_t file;
    intptr_t   hFile;
    char buf[MAX_LEN];

    if (_chdir(root))
    {
        printf("打开文件夹失败: %s\n", root);
        return 1;
    }

    hFile = _findfirst("*.c", &file);
    while (_findnext(hFile, &file) == 0)
    {
        sprintf_s(buf, MAX_LEN, "%s\\%s", root, file.name);
        printf("%s\n", buf);
    }

    return 0;
}

下面是网友提供的方案,仅供参考:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
 
#define BUFSIZE 4096
#define LONG_DIR_NAME TEXT("c:\\longdirectoryname")
 
void _tmain(int argc, TCHAR *argv[])
{
    DWORD  retval=0;
    BOOL   success; 
    TCHAR  buffer[BUFSIZE]=TEXT(""); 
    TCHAR  buf[BUFSIZE]=TEXT(""); 
    TCHAR** lppPart={NULL};
 
   if( argc != 2 )
   {
      _tprintf(TEXT("Usage: %s [file]\n"), argv[0]);
      return;
   }
 
// Retrieve the full path name for a file. 
// The file does not need to exist.
 
    retval = GetFullPathName(argv[1],
                 BUFSIZE,
                 buffer,
                 lppPart);
     
    if (retval == 0) 
    {
        // Handle an error condition.
        printf ("GetFullPathName failed (%d)\n", GetLastError());
        return;
    }
    else 
    {
        _tprintf(TEXT("The full path name is:  %s\n"), buffer);
        if (lppPart != NULL && *lppPart != 0)
        {
            _tprintf(TEXT("The final component in the path name is:  %s\n"), *lppPart);
        }
    }
 
 
// Create a long directory name for use with the next two examples.
 
    success = CreateDirectory(LONG_DIR_NAME,
                              NULL);
 
    if (!success)
    {
        // Handle an error condition.
        printf ("CreateDirectory failed (%d)\n", GetLastError());
        return;
    }
 
 
// Retrieve the short path name.  
 
    retval = GetShortPathName(LONG_DIR_NAME,
                  buf,
                  BUFSIZE);
 
    if (retval == 0) 
    {
        // Handle an error condition.
         printf ("GetShortPathName failed (%d)\n", GetLastError());
         return;
    }
    else _tprintf(TEXT("The short name for %s is %s\n"), 
                  LONG_DIR_NAME, buf);
 
 
// Retrieve the long path name.  
 
    retval = GetLongPathName(buf,
              buffer,
              BUFSIZE);
 
    if (retval == 0) 
    {
        // Handle an error condition.
         printf ("GetLongPathName failed (%d)\n", GetLastError());
         return;
    }
    else _tprintf(TEXT("The long name for %s is %s\n"), buf, buffer);
 
// Clean up the directory.
 
    success = RemoveDirectory(LONG_DIR_NAME);
    if (!success)
    {
        // Handle an error condition.
        printf ("RemoveDirectory failed (%d)\n", GetLastError());
        return;
    }
}

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值