推荐.NET教程: ASP.NET C# 开发环境 Ajax教程 控件开发 统计报表 数据库 Web服务 安装部署 CommunityServer NHibernate DataGrid/GridView 实用代码 VS2005
示例源码 MVC/三层 SqlHelper 入门源码 开源 CMS Ajax/Atlas C#.net 毕业设计 源码 经典代码 商业 本站作品 持久层 随书源码 WebService 英文/汉化 Asp.net2.0

阿牛·乐园

每天进步一点点

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  80 随笔 :: 14 文章 :: 389 评论 :: 13 引用

2007年7月7日 #

     摘要: 编程语言:C++ 类 别:(其他) 主要功能:修复WebCompiler被某病毒后,无法打开电子书的问题 说明:有种病毒在电子书最后几个字节加上了一些代码. 先看一下文件最后4个字节 红线上的部分,是多出来的几个字节,就是它造成了电子书不能打开!! 由于我有好多电子书,要是手动改,也太麻烦了.所以写了一个工具,希望有遇到我和一样烦恼的人,可以重新修复电子书. 修复前要备份电子书呀,我不敢保证所... 阅读全文
posted @ 2007-07-07 12:03 阿牛 阅读(750) | 评论 (4)编辑

编程语言:C++
类 别:(实用算法)
主要功能:用C++语言实现目录文件的非递归遍历并用伪函数来进行文件操作

    在用进行文件操作时,少不了和目录的递归打交道,但我一般认为.递归算法比较慢.如果可以采用非递归实现,就不要递归.
   
    在非递归算法中,一般我们用一个队列来保存相应的数据.一会列出代码.
    还有一个问题,我们递归目录,无非是想对文件进行操作,或者想得到文件的一个列表. 这时,你可以会采用回调函数. 但在我看来,还有更好的实现文案,让"回调"函数是一个对象,就既可以实现回调,也可以保存数据,这就是C++语言的仿函数.今天,我们就用仿函数来对指定的文件进行操作,例如修改文件,或者得到文件列表.
   
    先看一下递归函数的实现:

bool FindPathFiles(LPCTSTR pPath, LPCTSTR pExt, bool includeSubdir,FileOperator* fileOperator]/* = NULL : 这个是仿函数的指针,一会用来对每个文件进行操作 */)
{
WIN32_FIND_DATA fd;
list<tstring> directories;  // 这里就是保存递归数据,保存目录
directories.push_back(tstring(pPath));

while(directories.size() > 0)
{
  tstring path 
= directories.front();
  directories.pop_front();
  tstring pathFind 
= path + _T("\\*");
  HANDLE hFind 
= NULL;
  hFind 
= ::FindFirstFile(pathFind.c_str(),&fd);
  
if (hFind == INVALID_HANDLE_VALUE) 
  
{
  
return false;
  }
 
  
bool bFinished = false;
  
while(!bFinished)
  
{
  
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  
{
    
//是目录,如果不是父目录或者当前目录,则把目录压要待处理的目录列表中.
   tstring strFileName(fd.cFileName);
    
if(includeSubdir)
    
{
    
if(strFileName != _T("."&& strFileName != _T(".."))
    
{
      directories.push_back(path 
+ _T("\\"+ strFileName);
    }

    }

  }

  
else
  
{
    
if(fileOperator)
    
{
    
// 目录操作
    (*fileOperator)(path, &fd);
   }

    
else
    
{
    wcout 
<< fd.cFileName << endl;
    }

  }

  BOOL bRet 
= ::FindNextFile(hFind,&fd);
  
if(!bRet)
  
{
    bFinished 
= true;
  }

  }

  ::FindClose(hFind);
}

return true;
}


上面的部分,着重说明非递归的实现要点,FileOperator* fileOperator,是仿函数的操作

    我们再来看一下,如何定义属全上面的仿函数,再写一个抽象基类:
class FileOperator
{
public:
virtual void operator()(const tstring& path, LPWIN32_FIND_DATA pfdd) = NULL;
}
;

    如果我们对每个文件进行操作,我们这样来定义,下面一个例子,是我对第个 EXE 文件进行一下修复操作:
class FixEBOOK : public FileOperator
{
protected:
bool TryFixVirusBook(tstring file)
{
  
return true;
}

public:
void operator()(const tstring& path, LPWIN32_FIND_DATA pfd)
{
  WIN32_FIND_DATA
& fd = *pfd;
  tstring file(path 
+ _T("\\"+ fd.cFileName);
  tstring fileExt 
= file.substr(file.rfind('.'));
  transform(fileExt.begin(),fileExt.end(),fileExt.begin(),tolower);
  
if(fileExt != _T(".exe"))
  
return;
  
  
//操作
  this->TryFixVirusBook(file);

  
return;
}

}


//////////////////////////////////////////////
//声明操作
FixEBOOK oper;
//遍历每个文件,进行操作
FindPathFiles(szCurrentPath,_T(".exe"),true&oper);

  如果我们想得到文件列表,则可以这样写:
class FileListOper : public FileOperator
{
protected:
list
<tstring> m_files;
list
<tstring>& GetFiles(voidconst
{
  
return m_files;
}

public:
void operator()(const tstring& path, LPWIN32_FIND_DATA pfd)
{
  WIN32_FIND_DATA
& fd = *pfd;
  tstring file(path 
+ _T("\\"+ fd.cFileName);

  m_files.push_back(file);
  
return;
}

}


//////////////////////////////////////////////////////////////////
//声明操作
FileListOper oper;
//遍历每个文件,进行操作
FindPathFiles(szCurrentPath,_T(".exe"),true&oper);

  
const  list<tstring>& files = oper.GetFiles();

    希望大家拍砖讨论
posted @ 2007-07-07 11:58 阿牛 阅读(709) | 评论 (0)编辑

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    
<title>Untitled</title>

</head>

<body>
<div style="position:absolute;width:100%;height:100%;overflow:hidden;background-color: #fff;filter:alpha(opacity=50);    opacity: 0.5;-moz-opacity:0.5;">
</div>
<div style="position:absolute;border:solid 1px blue;margin-top:20%;text-align:center;width:100%;">
    
<img src="#"/>正在处理中..
</div>
<div id="list">
    
<table id="Table1" cellspacing="0" cellpadding="0" align="center" border="0" width="100%">
        
<tr>
            
<td align="center">
                
<fieldset>
                    
<legend>查询条件</legend>
                    
<table id="Table2" cellspacing="0" cellpadding="0" width="98%" border="0">
                        
<tr>
                            
<td width="10%">
                                用户名称
</td>
                            
<td width="13%"><input name="ctl00$placeContent$UserList1$tbName" type="text" id="ctl00_placeContent_UserList1_tbName" /></td>                         
                            
<td width="10%">
                                用户卡号
</td>
                            
<td width="13%"><input name="ctl00$placeContent$UserList1$tbCardNumber" type="text" id="ctl00_placeContent_UserList1_tbCardNumber" /></td>                         
                            
<td width="10%">用户状态</td>
                            
<td width="13%">
                                
<select name="ctl00$placeContent$UserList1$ddlState" id="ctl00_placeContent_UserList1_ddlState">
        
<option selected="selected" value="0">正常</option>
        
<option value="1">禁用</option>
        
<option value="2">锁定</option>
        
<option value="3">删除</option>

    
</select>
                                
</td>
                        
</tr>
                        
<tr>
                            
<td colspan="6" style="height: 30px">
                                
<input type="submit" name="ctl00$placeContent$UserList1$btnSearch" value=" 查询 " id="ctl00_placeContent_UserList1_btnSearch" />
                                
</td>
                        
</tr>
                    
</table>
                
</fieldset>
            
</td>
        
</tr>
    
</table>


<div>

    
</div>

    
<!-- AspNetPager V6.0.0 for VS2005  Copyright:2003-2006 Webdiyer (www.webdiyer.com) -->
    
<!--记录总数只有一页,AspNetPager已自动隐藏,若需在只有一页数据时显示AspNetPager,请将AlwaysShow属性值设为true!-->
    
<!-- AspNetPager V6.0.0 for VS2005 End -->

    
</div>
<div id="operation">
    
<input type="button" value="返回" onclick="GoBack()" />
</div>

</body>
</html>
posted @ 2007-07-07 10:19 阿牛 阅读(314) | 评论 (0)编辑