STL实践(string&IO)

直接从文件中读取

#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;

}

 

缓存到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;
}

 

效率上讲,后面一种处理在release方式下比前者快了一倍,在debug下,快了14倍左右。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值