发现博客访问好象不行,写个小的工具,就是把c++代码中的//注释去掉,原因是如果下的程序编码和默认编码不同,往往因为注释编译通不过,或者被一些大哥的不断弱智注释搞得郁闷,好像别人都要剽窃他一样,怎么办,删,当然不能手动删,写个代码好了。
打开vc6.0,建一个基于dialog的mfc工程,做一个下面的界面
file按钮主要实现单个文件的删除,filegroup实现文件夹的删除。
先写一个scan函数,用于把传进去的文件,直接删除
注意不要误删"//"
void
scan(CString nm)
{
char buf[ 1024 ];
CString temp;
char * p = NULL;
int line = 0 ;
struct pbuf
{
char lbuf[ 1024 ];
};
FILE * fp;
char * name = new char [ 1024 ];
strcpy(name,nm);
fp = fopen(name, " rb " );
while (fgets(buf, 1024 ,fp))
{
line ++ ;
}
fseek(fp, 0 , 0 );
pbuf * pb = new pbuf[line + 1 ];
line = 0 ;
while (fgets(buf, 1024 ,fp))
{
p = strstr(buf, " // " );
if (p &&* (p - 1 ) != ' " ' )
{
* p = 0 ;
strcat(buf, " " );
}
temp = buf;
temp.TrimRight();
strcpy(pb[line].lbuf,temp);
line ++ ;
}
fclose(fp);
FILE * fo;
fo = fopen(name, " w " );
for ( int j = 0 ;j < line;j ++ )
{
fprintf(fp, " %s " ,pb[j].lbuf);
}
fclose(fo);
}
{
char buf[ 1024 ];
CString temp;
char * p = NULL;
int line = 0 ;
struct pbuf
{
char lbuf[ 1024 ];
};
FILE * fp;
char * name = new char [ 1024 ];
strcpy(name,nm);
fp = fopen(name, " rb " );
while (fgets(buf, 1024 ,fp))
{
line ++ ;
}
fseek(fp, 0 , 0 );
pbuf * pb = new pbuf[line + 1 ];
line = 0 ;
while (fgets(buf, 1024 ,fp))
{
p = strstr(buf, " // " );
if (p &&* (p - 1 ) != ' " ' )
{
* p = 0 ;
strcat(buf, " " );
}
temp = buf;
temp.TrimRight();
strcpy(pb[line].lbuf,temp);
line ++ ;
}
fclose(fp);
FILE * fo;
fo = fopen(name, " w " );
for ( int j = 0 ;j < line;j ++ )
{
fprintf(fp, " %s " ,pb[j].lbuf);
}
fclose(fo);
}
再写一个OnFile函数用于传入单文件名
void
CCodeclearDlg::OnFile()
{
CString filter;
filter = " CPP Files (*.cpp)| *.cpp|DSW Files (*.dsw)| *.dsw| All Files (*.*)| *.*|| " ;
CFileDialog myfile(TRUE,NULL, " *.cpp " ,NULL,filter,NULL);
if (myfile.DoModal() == IDOK)
{
scan(myfile.GetPathName());
}
else return ;
}
{
CString filter;
filter = " CPP Files (*.cpp)| *.cpp|DSW Files (*.dsw)| *.dsw| All Files (*.*)| *.*|| " ;
CFileDialog myfile(TRUE,NULL, " *.cpp " ,NULL,filter,NULL);
if (myfile.DoModal() == IDOK)
{
scan(myfile.GetPathName());
}
else return ;
}
写OnFileGroup函数的时候应该注意下
先用BrowseFolder生成浏览文件夹
(看不懂得话,看一下vckbase上的杨老师讲的com)
CString BrowseFolder(HWND hWnd, LPCTSTR lpTitle)
{
char szPath[MAX_PATH] = { 0 };
BROWSEINFO m_bi;
m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
m_bi.hwndOwner = hWnd;
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = lpTitle;
m_bi.lpfn = NULL;
m_bi.lParam = NULL;
m_bi.pszDisplayName = szPath;
LPITEMIDLIST pidl = ::SHBrowseForFolder( & m_bi );
if ( pidl )
{
if ( ! ::SHGetPathFromIDList ( pidl, szPath ) ) szPath[ 0 ] = 0 ;
IMalloc * pMalloc = NULL;
if ( SUCCEEDED ( ::SHGetMalloc( & pMalloc ) ) )
{
pMalloc -> Free( pidl );
pMalloc -> Release();
}
}
return szPath;
}
{
char szPath[MAX_PATH] = { 0 };
BROWSEINFO m_bi;
m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
m_bi.hwndOwner = hWnd;
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = lpTitle;
m_bi.lpfn = NULL;
m_bi.lParam = NULL;
m_bi.pszDisplayName = szPath;
LPITEMIDLIST pidl = ::SHBrowseForFolder( & m_bi );
if ( pidl )
{
if ( ! ::SHGetPathFromIDList ( pidl, szPath ) ) szPath[ 0 ] = 0 ;
IMalloc * pMalloc = NULL;
if ( SUCCEEDED ( ::SHGetMalloc( & pMalloc ) ) )
{
pMalloc -> Free( pidl );
pMalloc -> Release();
}
}
return szPath;
}
调用BrowseFolder获得文件夹路径
注意一下剔除掉子文件和*.clw'*
if(!finder.IsDirectory()&&dsa.Find(".rc")<0&&dsa.Find(".clw")<0)
void
CCodeclearDlg::OnFileGroup()
{
HWND hw = NULL;
LPCTSTR tl = " open what't you want " ;
CString path = BrowseFolder(hw,tl);
if (path == "" ) return ;
CFileFind finder;
path += " /*.* " ;
BOOL bWorking = finder.FindFile(path);
int i = 0 ;
while (bWorking)
{
bWorking = finder.FindNextFile();
CString dsa = (LPCTSTR) finder.GetFilePath();
if ( ! finder.IsDirectory() && dsa.Find( " .rc " ) < 0 && dsa.Find( " .clw " ) < 0 )
{
scan(dsa);
}
i ++ ;
}
}
{
HWND hw = NULL;
LPCTSTR tl = " open what't you want " ;
CString path = BrowseFolder(hw,tl);
if (path == "" ) return ;
CFileFind finder;
path += " /*.* " ;
BOOL bWorking = finder.FindFile(path);
int i = 0 ;
while (bWorking)
{
bWorking = finder.FindNextFile();
CString dsa = (LPCTSTR) finder.GetFilePath();
if ( ! finder.IsDirectory() && dsa.Find( " .rc " ) < 0 && dsa.Find( " .clw " ) < 0 )
{
scan(dsa);
}
i ++ ;
}
}
好了运行下把,就拿自己做实验,是不是很爽