Ϊʲôͬһ¸ö³ÌÐòÔÚcmdºÍvs2012Àïµ÷ÊÔ½á¹û²»Í¬
ÏÂÃæÊÇͬһ¸ö³ÌÐòµÄÔÚcmdºÍvs2012ÀïµÄ²»Í¬ÏÔʾ½á¹û£¬»¹ÓÐΪʲô²»Í¬Â·¾¶ÏÂÓеÄtxt¿ÉÒÔ´ò¿ªÓеĴò¿ªÊ§°ÜÄØ£¿ÇóÖ¸½Ì°¡´úÂ룺
#include <io.h>
#include <cstring>
#include <cerrno>
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;
// 0: ³É¹¦
// -1: Îļþ´ò¿ªÊ§°Ü
// -2: δÕÒµ½intÀàÐÍÊýÖµ
int foo( const char* filename, int& maxval );
int main()
{
string fileName = "D:\\VS2012\\CPP\\linemax1\\linemax1\\*.txt";
int maxval_of_allfile;
vector<string> allfile_with_maxval;
_finddata_t sFind;
long handle = _findfirst(fileName.c_str(), &sFind);
bool bfound = false;
if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
}
do
{
if( (sFind.attrib&_A_SUBDIR) == 0 )
{
bfound = true;
cout << sFind.name << ":\n";
int maxval;
switch( foo(sFind.name,maxval) )
{
case 0:
cout << "\t[OUTPUT] ×î´óÖµ = " << maxval << '\n';
if(maxval>10||maxval<-10)
{
cout<<"Beyond"<<endl;
cout<<'\a'<<endl;
}
if( allfile_with_maxval.empty() )
{
maxval_of_allfile = maxval;
allfile_with_maxval.push_back( sFind.name );
}
else if( maxval == maxval_of_allfile )
{
allfile_with_maxval.push_back( sFind.name );
}
else if( maxval > maxval_of_allfile )
{
maxval_of_allfile = maxval;
allfile_with_maxval.clear();
allfile_with_maxval.push_back( sFind.name );
}
break;
case -1:
cout << "\t[ERROR] Îļþ´ò¿ªÊ§°Ü.\n";
break;
case -2:
cout << "\t[WARNING] δÕÒµ½intÀàÐÍÊýÖµ.\n";
break;
}//cout <<sFind.name <<endl;
}
} while (_findnext(handle, &sFind) == 0);
//system("pause");
//return true;
if( allfile_with_maxval.empty() )
cout << "ËùÓÐÆ¥ÅäµÄÎļþÖнÔÎÞintÀàÐÍ.\n";
else
{
cout << "ÔÚËùÓÐÆ¥ÅäµÄÎļþÖУ¬ÓµÓÐ×î´óÖµ(" << maxval_of_allfile << ")µÄÎļþÊÇ:\n";
for( size_t i=0; i!=allfile_with_maxval.size(); ++i )
cout << '\t' << allfile_with_maxval[i] << '\n';
}
system("pause");
return 0;
}
int foo( const char* filename, int& maxval )
{
maxval = std::numeric_limits<int>::min();
ifstream is( filename );
if( !is )
return -1;
bool bfound = false;
for( string s; is>>s; )
{
istringstream ss( s );
int val;
if( ss>>val && ss.eof() )
{
bfound = true;
maxval = max( maxval, val );
}
}
if( !bfound )
return -2;
return 0;
}