getKey

本文介绍了一个C++程序,该程序用于搜索指定目录下符合特定模式的文件,并读取这些文件的内容进行关键字查找与处理。程序使用了标准输入输出流、文件操作及字符串处理等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <fstream>
#include <afx.h>
#include <string>

using namespace std;

int WCharToChar(UINT PageCode, std::wstring strWChar, std::string &strChar);


int findKey(const string key);


int main()
{

    CFileFind finder; 

	
    // build a string with wildcards 
   CString propertiesdir("class\\*.properties"); 

   // start working for files 
   BOOL bWorking = finder.FindFile(propertiesdir); 

   cout<< bWorking <<endl;

   while (bWorking) 
   { 
       bWorking = finder.FindNextFile(); 

	   CString str = finder.GetFilePath(); 
	    CString strName = finder.GetFileName(); 
	    cout << str << endl; 

	   string propertiesfile;
	   string fileName;

	   WCharToChar( CP_ACP, str.GetBuffer(), propertiesfile);
	   WCharToChar( CP_ACP, strName.GetBuffer(), fileName);
     
    //   cout << propertiesfile << endl; 

	   ifstream infile(propertiesfile.c_str(), ios::in);
       if( !infile)
	   {
		   cerr<< "oops! unable to open file " <<propertiesfile<<endl;
		   exit(0);
	   }
	   ofstream outfile(("new\\"+fileName).c_str(),ios::out);
	    if( !outfile)
	   {
		   cerr<< "oops! unable to open file" <<"new\\"+propertiesfile<<endl;
	   }

	   string textline;

	   while( getline(infile, textline, '\n'))
	   {
		   if(textline=="" || textline.find_first_of("#")==0)
			   outfile << textline <<endl;
		   else
		   {
			   string::size_type pos = textline.find_first_of("=");
			   string key = textline.substr(0,pos);
			   cout<<"key:"<<key<<"  find:"<<findKey(key)<<endl;
			   if(findKey(key) !=0) 
			   {
					outfile << textline <<endl;
			   }
			   else
			   {
				   outfile <<"###"<< textline <<"###"<<endl;
			   }
		   }
	   }
   } 

   finder.Close(); 
} 


int findKey(const string key)
{
	 CFileFind finder; 

	
    // build a string with wildcards 
   CString jspdir("jsp\\*.jsp"); 

   // start working for files 
   BOOL bWorking = finder.FindFile(jspdir); 

   while (bWorking) 
   { 
       bWorking = finder.FindNextFile(); 

	   CString str = finder.GetFilePath(); 

	   string jspfile;

	   WCharToChar( CP_ACP, str.GetBuffer(), jspfile);
     
       cout << jspfile << endl; 

	   ifstream infile(jspfile.c_str(), ios::in);
       if( !infile)
	   {
		   cerr<< "oops! unable to open file " <<jspfile<<endl;
		   exit(0);
	   }

	   string textline;

	   while( getline(infile, textline, '\n'))
	   {
		
		  string::size_type pos = textline.find("\""+key+"\"");
		  if(pos != string::npos)
		  {
			  finder.Close(); 
			  return 1;
		  }
	   }
   } 
	
   finder.Close(); 
   return 0;
}

int WCharToChar(UINT PageCode, std::wstring strWChar, std::string &strChar)
{
 strChar.clear();
 int ret = 0;
 int nSize = (int)strWChar.length()*2;
 static char pszDest[1024*10];
 if( pszDest )
 {
  memset( pszDest, 0, nSize+1 );
  ret = WideCharToMultiByte(PageCode,NULL,strWChar.c_str(),-1,pszDest,nSize,NULL,FALSE);
  pszDest[nSize] = '\0';
  strChar = pszDest;
 }
 return ret;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值