直接从文件中读取
#include
<
iostream
>
#include < string >
#include < fstream >
#include < cstdlib >
#include < windows.h >
using namespace std;
inline bool getItem( string & wholeFile, string & item, char separator, int & stat);
int main()
... {
ifstream fin;
fin.open("Temp.cfg");
if( !fin.is_open())
...{
cerr<<"Can't open the file!"<<endl;
return 0;
//exit();
}
string strBeginMark = "----Begin of File----";
string strEndMark = "----";//"----End of File----";
string item;
string element;
string wholeFile;
char szElement[20] = ...{0};
int count = 0;
getline(fin,wholeFile,'E'); //将文件中的内容缓存到一个string 对象中
//#ifdef _DEBUG
DWORD dwStart = GetTickCount();
//#endif
while(true)
...{
if( item == strEndMark )
...{
break;
}
if(item == strBeginMark || item == " 权限树")
...{
getItem(wholeFile ,item,' ',count);
}
else
...{
int nBegin = 0;
int nEnd = 0;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
while(nEnd >= 0)
...{
// cout << element << endl;
element.clear();
nBegin = nEnd +1;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
}
getItem(wholeFile ,item,' ',count);
}
}
// cout <<"OK !"<<endl;
//#ifdef _DEBUG
dwStart = GetTickCount() - dwStart;
//#endif
cout<< dwStart;
fin.close();
return 0;
}
inline bool getItem( string & wholeFile, string & item, char separator, int & stat)
... {
int nBegin = stat +1;
int nEnd = 0;
int count = 0;
item.clear();
nEnd = wholeFile.find_first_of(separator ,nBegin);
item = wholeFile.substr(nBegin,nEnd-nBegin );
stat = nEnd;
return true;
}
#include < string >
#include < fstream >
#include < cstdlib >
#include < windows.h >
using namespace std;
inline bool getItem( string & wholeFile, string & item, char separator, int & stat);
int main()
... {
ifstream fin;
fin.open("Temp.cfg");
if( !fin.is_open())
...{
cerr<<"Can't open the file!"<<endl;
return 0;
//exit();
}
string strBeginMark = "----Begin of File----";
string strEndMark = "----";//"----End of File----";
string item;
string element;
string wholeFile;
char szElement[20] = ...{0};
int count = 0;
getline(fin,wholeFile,'E'); //将文件中的内容缓存到一个string 对象中
//#ifdef _DEBUG
DWORD dwStart = GetTickCount();
//#endif
while(true)
...{
if( item == strEndMark )
...{
break;
}
if(item == strBeginMark || item == " 权限树")
...{
getItem(wholeFile ,item,' ',count);
}
else
...{
int nBegin = 0;
int nEnd = 0;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
while(nEnd >= 0)
...{
// cout << element << endl;
element.clear();
nBegin = nEnd +1;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
}
getItem(wholeFile ,item,' ',count);
}
}
// cout <<"OK !"<<endl;
//#ifdef _DEBUG
dwStart = GetTickCount() - dwStart;
//#endif
cout<< dwStart;
fin.close();
return 0;
}
inline bool getItem( string & wholeFile, string & item, char separator, int & stat)
... {
int nBegin = stat +1;
int nEnd = 0;
int count = 0;
item.clear();
nEnd = wholeFile.find_first_of(separator ,nBegin);
item = wholeFile.substr(nBegin,nEnd-nBegin );
stat = nEnd;
return true;
}
缓存到string中的处理方式
#include
<
iostream
>
#include < string >
#include < fstream >
#include < cstdlib >
#include < windows.h >
using namespace std;
int main()
... {
ifstream fin;
fin.open("Temp.cfg");
if( !fin.is_open())
...{
cerr<<"Can't open the file!"<<endl;
return 0;
//exit();
}
string strBeginMark = "----Begin of File----";
string strEndMark = "----End of File----";
string item;
string element;
char szElement[20] = ...{0};
int count = 0;
#ifdef _DEBUG
DWORD dwStart = GetTickCount();
#endif
getline(fin,item,' ');
while(fin)
...{
if(item == strBeginMark || item == strEndMark || item == ""||item == " 权限树")
...{
getline(fin,item,' ');
}
else
...{
count++;
//cout <<count << ": "<<item <<endl;
int nBegin = 0;
int nEnd = 0;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
while(nEnd > 0)
...{
nBegin = nEnd +1;
nEnd = item.find_first_of(';',nBegin+1);
element = item.substr(nBegin,nEnd - nBegin);
//cout << element << endl;
element.clear();
}
getline(fin,item,' ');
}
}
#ifdef _DEBUG
dwStart = GetTickCount() - dwStart;
#endif
cout <<"OK !"<<endl;
fin.close();
return 0;
}
#include < string >
#include < fstream >
#include < cstdlib >
#include < windows.h >
using namespace std;
int main()
... {
ifstream fin;
fin.open("Temp.cfg");
if( !fin.is_open())
...{
cerr<<"Can't open the file!"<<endl;
return 0;
//exit();
}
string strBeginMark = "----Begin of File----";
string strEndMark = "----End of File----";
string item;
string element;
char szElement[20] = ...{0};
int count = 0;
#ifdef _DEBUG
DWORD dwStart = GetTickCount();
#endif
getline(fin,item,' ');
while(fin)
...{
if(item == strBeginMark || item == strEndMark || item == ""||item == " 权限树")
...{
getline(fin,item,' ');
}
else
...{
count++;
//cout <<count << ": "<<item <<endl;
int nBegin = 0;
int nEnd = 0;
nEnd = item.find_first_of(';',nBegin);
element = item.substr(nBegin,nEnd - nBegin);
while(nEnd > 0)
...{
nBegin = nEnd +1;
nEnd = item.find_first_of(';',nBegin+1);
element = item.substr(nBegin,nEnd - nBegin);
//cout << element << endl;
element.clear();
}
getline(fin,item,' ');
}
}
#ifdef _DEBUG
dwStart = GetTickCount() - dwStart;
#endif
cout <<"OK !"<<endl;
fin.close();
return 0;
}
效率上讲,后面一种处理在release方式下比前者快了一倍,在debug下,快了14倍左右。