⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listdir.cpp

📁 VC源代码大全(精华版)
💻 CPP
字号:
/*
    ListDir.cpp - - lists the DOS and Windows file name of
                    files in a specified directory.
 */
#include    <stdio.h>
#include    <windows.h>
#include    <windef.h>
#include    <winbase.h>
#include    "utility.h"

bool ListFiles (char *pszDir);

main (int argc, char *argv[])
{
    char *pszProgName = GetProgName (argv[0]);
    if (argc < 2)
    {
        fprintf (stderr, "%s: Enter a directory path\n",
                 pszProgName);
        return (-1);
    }
    if (!ListFiles (argv[1]))
    {
        fprintf (stderr, "%s: Cannot open path\n",
                 pszProgName);
        return (-1);
    }
    return (0);
}

bool ListFiles (char *pszDir)
{
char    Path[MAX_PATH];
HANDLE  hFile;
WIN32_FIND_DATA   fileData;

    strcpy (Path, pszDir);
    if ((Path[strlen(Path) - 1] == '\\') ||
        (Path[strlen(Path) - 1] == '/'))
        strcat (Path, "*.*");
    else
        strcat (Path, "\\*.*");

//  If we can't find the first file, we can't continue
    if ((hFile = FindFirstFile(Path, &fileData))
            == INVALID_HANDLE_VALUE)
    {
        return (false);
    }
    do
    {
//  Don't list directories
        if (fileData.dwFileAttributes & 
                FILE_ATTRIBUTE_DIRECTORY)
            continue;
//  If no DOS name, it's same as Windows name
        if (!strlen (fileData.cAlternateFileName))
        {
            strncpy (fileData.cAlternateFileName,
                     fileData.cFileName, 13);
//  DOS name is always caps
            _strupr (fileData.cAlternateFileName);
        }
//  List the file names
        printf ("%-16s%s\n", fileData.cAlternateFileName,
                             fileData.cFileName);
    } while (FindNextFile (hFile, &fileData));
//  Close the find functions
    FindClose (hFile);
    return (true);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -