| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1245 人关注过本帖
标题:刚学C语言老师让我们把代码注释起来结果发过来没看懂
只看楼主 加入收藏
叫什么好捏
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2021-4-22
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
刚学C语言老师让我们把代码注释起来结果发过来没看懂
#ifdef _UNICODE
#undef _UNICODE
#endif // _UNICODE

#ifdef UNICODE
#undef UNICODE
#endif // _UNICODE

#include<Windows.h>
#include<stdio.h>
#include<string.h>



#define ONE 1
#define BUFSIZE 1024

     
int ListDirectoryContents( const char *path );



int main()
{
   
    char path[BUFSIZE] = "D:\\data\\exam\\1";   
   
    ListDirectoryContents( path );
   
   //结束
    return 0;
}


   
int ListDirectoryContents( const char *path )
{
   
    WIN32_FIND_DATA fileData = { 0 };
    HANDLE h = NULL;
    int exit = 0;
    char newPath[BUFSIZE] = {0};
    char *addressOfLastCharacter = NULL;

    //字符串格式化
    sprintf( newPath, "%s\\*.*", path );

    while(ONE)
    {
        
        if( ( h = FindFirstFile( newPath, &fileData ) ) == INVALID_HANDLE_VALUE )
        {
            printf( "Path not found: [%s]\n", path );   
            exit = 1;
            break;
        }
        do
        {
            
            if( ( strcmp( fileData.cFileName, ".") != 0 ) && ( strcmp( fileData.cFileName, ".." ) != 0 ) )
            {
                sprintf( newPath, "%s\\%s", path, fileData.cFileName );

            
                if( ( fileData.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) != 0 )
                {
                    ListDirectoryContents( newPath );
                }
                else
                {                    
                    printf( "%s\n", fileData.cFileName );
                }
            }
        }        
        while( FindNextFile( h, &fileData ) );
   
        break;
        
    }
   
    FindClose( h );
   
    return exit;
搜索更多相关主题的帖子: exit path int 老师 char 
2021-04-22 14:48
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1481
专家分:9055
注 册:2010-3-16
收藏
得分:10 
程序代码:
#ifdef _UNICODE
#undef _UNICODE
#endif // _UNICODE

#ifdef UNICODE
#undef UNICODE
#endif // _UNICODE

#include <Windows.h>
#include <stdio.h>
#include <string.h>

#define ONE 1
#define BUFSIZE 1024

int ListDirectoryContents(const char *path);

int main()
{

    char path[BUFSIZE] = "D:\\data\\exam\\1";

    ListDirectoryContents(path);

    //结束
    return 0;
}

//查找path下的所有文件
int ListDirectoryContents(const char *path)
{

    WIN32_FIND_DATA fileData = {0}; //文件属性
    /*
    WIN32_FIND_DATA内容如下
typedef struct _WIN32_FIND_DATA {
    DWORD dwFileAttributes; //文件属性
    FILETIME ftCreationTime; // 文件创建时间
    FILETIME ftLastAccessTime; // 文件最后一次访问时间
    FILETIME ftLastWriteTime; // 文件最后一次修改时间
    DWORD nFileSizeHigh; // 文件长度高32位
    DWORD nFileSizeLow; // 文件长度低32位
    DWORD dwReserved0; // 系统保留
    DWORD dwReserved1; // 系统保留
    TCHAR cFileName[ MAX_PATH ]; // 长文件名
    TCHAR cAlternateFileName[ 14 ]; // 8.3格式文件名
} 
    */
    HANDLE h = NULL;             //搜索句柄
    int exit = 0;                //退出代码
    char newPath[BUFSIZE] = {0}; //搜索路径
    char *addressOfLastCharacter = NULL;

    //字符串格式化
    sprintf(newPath, "%s\\*.*", path);

    while (ONE)
    {

        if ((h = FindFirstFile(newPath, &fileData)) == INVALID_HANDLE_VALUE) //查找newPath里的文件,INVALID_HANDLE_VALUE表示出错
        {
            printf("Path not found: [%s]\n", path);
            exit = 1;
            break;
        }

        do
        {

            if ((strcmp(fileData.cFileName, ".") != 0) && (strcmp(fileData.cFileName, "..") != 0)) //跳过当前和上级目录,避免重复查找
            {
                sprintf(newPath, "%s\\%s", path, fileData.cFileName); //组合文件完整路径

                if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) //如果是文件夹,则查找这个文件夹里的文件
                {
                    ListDirectoryContents(newPath);
                }
                else
                {
                    printf("%s\n", fileData.cFileName); //如果是文件,则输出文件名
                }
            }

        } while (FindNextFile(h, &fileData)); //查找下一个文件

        break;
    }

    FindClose(h); //关闭搜索句柄

    return exit;
}
2021-04-23 00:02
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
首先,“#ifdef _UNICODE
#undef _UNICODE
#endif // _UNICODE”这一套玩意儿是不对的,正确的做法是应该用 WIN32_FIND_DATAA/FindFirstFileA/FindNextFileA 替代 WIN32_FIND_DATA/FindFirstFile/FindNextFile。

C++标准库中包含文件系统的功能,例如
程序代码:
#include <iostream>
#include <string>
#include <string_view>
#include <queue>
#include <filesystem>
using namespace std;
using namespace std::filesystem;

bool ListDirectoryContents( std::string_view folder )
{
    try {
        path p = canonical(folder);
        if( !is_directory(p) )
            return false;

        cout << p << ":\n";
        queue<directory_entry> folders;
        for( folders.push(directory_entry(p)); !folders.empty(); folders.pop() )
        {
            for( const auto& sub : directory_iterator(folders.front()) )
            {
                if( sub.is_directory() )
                    folders.push( sub );
                else if( sub.is_regular_file() )
                    cout << '\t' << relative(sub,p) << '\n';
            }
        }
    }
    catch( filesystem_error err ) {
        return false;
    }
    return true;
}

int main( void )
{
    ListDirectoryContents( "D:\\data\\exam\\1" );
}


一种可能的输出是:
"D:\\data\\exam\\1":
        "a.txt"
        "a\\b.txt"
        "a\\b\\新建文本文档.txt"
2021-04-25 09:26
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
两个斜杆太难看,改一下代码

程序代码:
#include <iostream>
#include <string>
#include <string_view>
#include <queue>
#include <filesystem>
using namespace std;
using namespace std::filesystem;

bool ListDirectoryContents( std::string_view folder )
{
    try {
        path p = canonical(folder);
        if( !is_directory(p) )
            return false;

        cout << p.string() << ":\n";
        queue<directory_entry> folders;
        for( folders.push(directory_entry(p)); !folders.empty(); folders.pop() )
        {
            for( const auto& sub : directory_iterator(folders.front()) )
            {
                if( sub.is_directory() )
                    folders.push( sub );
                else if( sub.is_regular_file() )
                    cout << '\t' << relative(sub,p).string() << '\n';
            }
        }
    }
    catch( const filesystem_error& ) {
        return false;
    }
    return true;
}

int main( void )
{
    ListDirectoryContents( "D:\\data\\exam\\1" );
}


一种可能的输出是:
D:\data\exam\1:
        a.txt
        a\b.txt
        a\b\新建文本文档.txt
2021-04-26 08:36
快速回复:刚学C语言老师让我们把代码注释起来结果发过来没看懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019780 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved