zoukankan      html  css  js  c++  java
  • 代码行数统计(指定目录下所有文件的Line)

    • 遍历文件夹计算文件行数(Windows)

    主要使用的是 FindFirstFile、 FindNextFile函数寻找子目录下的文件,使用 WIN32_FIND_DATA(文件属性) 结构体 

    #define _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    
    char localpath[250] = {0};
    int AllCodeLine = 0;
    int fileNum = 0;
    
    void WriteErrToOutputFlie(char *inputpath , char *ErrMessage)
    {
        FILE *fd;
        char buffer[250] = {0};
    
        sprintf(buffer, "%s\output.txt", inputpath);
    
        fd = fopen(buffer, "w");
        fputs(ErrMessage, fd);
    }
    
    int CreteOutputFlie(char *inputpath)
    {
        FILE *fd;
        char buffer[250] = {0};
    
        sprintf(buffer, "%s\output.txt", inputpath);
    
        fd = fopen(buffer, "w+");
        if(NULL == fd)
        {
            return -1;
        }
    
        fclose(fd);
        return 0;
    }
    
    int File_Check_operation(char *inputpath, char *outputpath, FILE* writefd)
    {
        HANDLE hFile;
        FILE *readfd;
        int flieline = 0;
        char ErrMess[250] = {0};
        char buffer[250] = {0};
        char szSubPath[250] = {0};
        WIN32_FIND_DATA pNextInfo;
    
    
        sprintf(buffer, "%s\*", inputpath);
    
        if( (NULL == inputpath) || (NULL == outputpath))
        {
            return -1;
        }
    
        hFile = FindFirstFile(buffer,&pNextInfo);
        if(!hFile)
        {
            return -2;
        }
    
        while(FindNextFile(hFile,&pNextInfo))  //loop local path,find subpath
        {
            
    
            if(strcmp(pNextInfo.cFileName, "..") == 0 || strcmp(pNextInfo.cFileName, ".") == 0 || strcmp(pNextInfo.cFileName, "output.txt") == 0)  // filter . and ..
            {
                    continue;
            }
    
            if(pNextInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)  //Check file is a directory
            {
                sprintf(szSubPath, "%s\%s", inputpath, pNextInfo.cFileName);
                File_Check_operation(szSubPath, outputpath, writefd);
            }
            else
            {
                //except image flie
                if( strcmp( &(pNextInfo.cFileName[strlen(pNextInfo.cFileName) - 3]), "png") == 0 ||
                    strcmp( &(pNextInfo.cFileName[strlen(pNextInfo.cFileName) - 3]), "gif") == 0 ||
                    strcmp( &(pNextInfo.cFileName[strlen(pNextInfo.cFileName) - 3]), "swf") == 0 )
                {
                    continue;
                }
    
                fileNum++;            //file num plus
    
                memset(buffer, 0, sizeof(buffer));
                sprintf(buffer, "%s\%s", inputpath, pNextInfo.cFileName);
    
                readfd = fopen(buffer, "r");
                if( readfd == NULL )
                {
                    sprintf(ErrMess, "%s", "can not fopen file %s\%s", inputpath, pNextInfo.cFileName);
                    printf(ErrMess);
                    WriteErrToOutputFlie(localpath, ErrMess);
                }
    
                while(!feof(readfd))
                {
                    if(fgetc(readfd) == '
    ')
                        flieline++;
                }
                flieline++;           //Last line
    
                fclose(readfd);
    
                fprintf(writefd, "	%s", pNextInfo.cFileName);
                fprintf(writefd, ":%d
    ", flieline);
                
                AllCodeLine += flieline;
            }
            flieline = 0;
    
        }
    }
    
    int main(int argc, char *argv[])
    {
        int ret = 0;
        char ErrMess[250] = {0};
        char buffer[250] = {0};
        FILE *writefd;
    
        memset(localpath, 0, sizeof(localpath));
        
        if(argc != 2)
        {
            exit(1);
        }
    
        strncpy_s(localpath, sizeof(localpath), argv[1], strlen(argv[1]));
    
        ret = CreteOutputFlie(localpath);
        if(ret != 0)
        {
            sprintf(ErrMess, "%s", "main call CreteOutputFlie failed!");
            printf(ErrMess);
            WriteErrToOutputFlie(localpath, ErrMess);
        }
    
        memset(buffer, 0, sizeof(buffer));
        sprintf(buffer, "%s\%s", localpath, "output.txt");
    
        writefd = fopen(buffer, "w");
    
        ret = File_Check_operation(localpath, localpath, writefd);
        if(ret != 0)
        {
            sprintf(ErrMess, "%s", "main call File_Check_operation failed!");
            printf(ErrMess);
            WriteErrToOutputFlie(localpath, ErrMess);
        }
    
        fprintf(writefd, "
    
     AllCodeLine is : %d", AllCodeLine);
    
        fclose(writefd);
    
        return 0;
    }
    

      

  • 相关阅读:
    PPT1 例1
    皇宫看守 树形DP
    没有上司的晚会 树形DP
    将功补过 树形DP
    战略游戏 树形DP
    选课 树形DP
    二叉苹果树 树形DP
    GDOI2016总结
    加分二叉树 树形DP
    [注意]未做的题(最短路)
  • 原文地址:https://www.cnblogs.com/xuyong437/p/11209114.html
Copyright © 2011-2022 走看看