用C语言编写统计指定目录中*.C,*CPP文件代码行数
用C语言编写
统计指定目录中包括子目录 *.C,*.CPP,*.h文件代码行数
不知道大家以前编过这样的程序,谢谢帮助
[解决办法]
void VolTdy::browse(string strdir) //strdir, for directory, sample "F:\* "
{
WIN32_FIND_DATAA wfd;
HANDLE m_hFle = FindFirstFile(strdir.c_str(), &wfd);
strdir.resize(strdir.size() - 1);
DWORD errCod = 0;
if (INVALID_HANDLE_VALUE != m_hFle)
{
do
{
string str_fullName;
errCod = 0;
//directory dealed code
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
string tmp = wfd.cFileName;
if ((tmp != ". ") && (tmp != ".. "))
{
string stedbg = wfd.cFileName;
str_fullName = strdir + stedbg;
//for desturctor run
{
MovFle mvf;
fleLog.open( "volLog.txt ", ios_base::app);
fleLog < < str_fullName < < endl;
if(mvf.init(str_fullName, m_ptr))
{
errCod = 0;
if(mvf.analysis(ull_usedClusters, errCod))
{
errCod = 0;
if(mvf.move(m_hVolum, m_mapLst, errCod));
else fleLog < < " mov -> " < < str_fullName < < endl < < "errCod " < < " = " < < errCod < < endl;
}
else
fleLog < < " ana -> " < < str_fullName < < endl < < "errCod " < < " = " < < errCod < < endl;
}
else fleLog < < " ini -> " < < str_fullName < < endl;
fleLog.close();
}
str_fullName += "\\* ";
browse(str_fullName);
}
}
//file dealed code
else
{
string stedbg = wfd.cFileName;
str_fullName = strdir + stedbg;
MovFle mvf;
fleLog.open( "volLog.txt ", ios_base::app);
fleLog < < str_fullName < < endl;
if(mvf.init(str_fullName, m_ptr))
{
errCod = 0;
if(mvf.analysis(ull_usedClusters, errCod))
{
errCod = 0;
//dbg
//cout < < "start mov " < < str_fullName < < endl;
if(mvf.move(m_hVolum, m_mapLst, errCod));
else fleLog < < " mov -> " < < str_fullName < < endl < < "errCod " < < " = " < < errCod < < endl;
}
else
fleLog < < " ana -> " < < str_fullName < < endl < < "errCod " < < " = " < < errCod < < endl;
}
else fleLog < < " ini -> " < < str_fullName < < endl;
fleLog.close();
}
} while(FindNextFile(m_hFle, &wfd));
}
if (INVALID_HANDLE_VALUE != m_hFle)
{
FindClose(m_hFle);
}
}
大致框架即这
[解决办法]
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <stdlib.h>
typedef unsigned char BYTE;
#define MAXBUFF 1024
#define DELIMER 0x0d
void errprint(const char * msg)
{
printf( "%s\n ",msg);
exit(1);
}
long file_len(const char *filename)
{
long len;
FILE *fp;
fp=fopen(filename, "rb ");
if(!fp)errprint( "打开文件出错 ");
fseek(fp,0,SEEK_END);
len=ftell(fp);
fclose(fp);
return len;
}
int check(const char *file)
{
const char *p=file;
while(*p)++p;
while(*p!= '. '&&p!=file)--p;
if(p==file)return 0;
else
{
if(!stricmp(p, ".c ")||!stricmp(p, ".cpp ")||!stricmp(p, ".h "))
{
return 1;
}
else return 0;
}
}
void print_lines(const char *filename,FILE *output)
{
int i,j;
int num,num1;
long lines=1;
BYTE buffer[MAXBUFF];
FILE *fp;
num=file_len(filename);
if(!num)
{
printf( "%s\t%d\n ",filename,0);
fprintf(output, "%s\t%d\n ",filename,0);
return;
}
num1=num%MAXBUFF;
num/=MAXBUFF;
fp=fopen(filename, "rb ");
for(i=0;i <num;++i)
{
if(fread(buffer,MAXBUFF,1,fp)!=1)
{
errprint( "读文件出错 ");
}
for(j=0;j <MAXBUFF;++j)
{
if(buffer[j]==DELIMER)
{
++lines;
}
}
}
if(fread(buffer,1,num1,fp)!=(unsigned int)num1)
{
errprint( "读文件出错 ");
}
for(i=0;i <num1;++i)
{
if(buffer[i]==DELIMER)
{
++lines;
}
}
printf( "%s\t%d\n ",filename,lines);
fprintf(output, "%s\t%d\n ",filename,lines);
fclose(fp);
}
void file_tran(const char *path,FILE *output)
{
char path_buffer[256]={0};
char path_buffer1[256];
struct _finddata_t filefind;
strcpy(path_buffer,path);
strcat(path_buffer, "\\*.* ");
if(!path_buffer1)
{
fclose(output);
errprint( "内存不足 ");
}
int done=0,handle;
if((handle=_findfirst(path_buffer,&filefind))==-1)
{
return;
}
while(!(done=_findnext(handle,&filefind)))
{
if(!strcmp(filefind.name, ".. "))
{
continue;
}
strcpy(path_buffer1,path);
strcat(path_buffer1, "\\ ");
strcat(path_buffer1,filefind.name);
if ((16==filefind.attrib||48==filefind.attrib))
{
file_tran(path_buffer1,output);
}
else
{
if(check(path_buffer1))
{
print_lines(path_buffer1,output);
}
}
}
_findclose(handle);
}
int main()
{
FILE *output;
output=fopen( "result.txt ", "w ");
file_tran( "C:\\Documents and Settings\\user\\桌面\\测试的一些小代码 ",output);
fclose(output);
system( "PAUSE ");
return 0;
}
[解决办法]
应你要求,使用findfirstfile
c++ (要c的话,改改就好了)
#include "windows.h "
#include "stdio.h "
#include "iostream "
#include "string "
#include "fstream "
using namespace std;
#define TestDir "D:\\test_line\\* "
struct Info {
int lines; // 行熟
int c_mun; // c / cpp 文件个数
};
void calc_lines(const char strPath[], Info& info)
{
string str;
ifstream file(strPath);
while (!file.fail())
{
getline(file, str);
info.lines++;
}
}
void find_match_file(const char strPath[], Info& info)
{
WIN32_FIND_DATAA wfd;
HANDLE hHandle = FindFirstFile(strPath, &wfd);
if (hHandle == INVALID_HANDLE_VALUE)
return;
while (FindNextFile(hHandle, &wfd))
{
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (stricmp(wfd.cFileName, ". ") && stricmp(wfd.cFileName, ".. "))
find_match_file(wfd.cFileName, info);
}
else
{
size_t len = strlen(wfd.cFileName);
if (len > 4)
{
if (
!stricmp(&wfd.cFileName[len - 3], "cpp ") ||
!stricmp(&wfd.cFileName[len - 1], "c ")
)
{
info.c_mun++;
calc_lines(wfd.cFileName, info);
}
}
else if (len > 2)
{
if (!stricmp(&wfd.cFileName[len - 1], "c "))
{
info.c_mun++;
calc_lines(wfd.cFileName, info);
}
}
}
}
FindClose(hHandle);
}
int main()
{
Info info = {0, 0};
find_match_file(TestDir, info);
cout < < "total c//cpp file : " < <info.c_mun < <endl;
cout < < "total lines : " < <info.lines < <endl;
return 0;
}