从文件中读n个取数字到RArray中

来源:百度文库 编辑:神马文学网 时间:2024/04/30 09:40:41
// Reads numbers from an input file into an RArray
void CActiveBubbleSorter::ReadNumbersFromFileL()
{
// Read contents of the input file into a descriptor
RFs fs;
User::LeaveIfError(fs.Connect());
CleanupClosePushL(fs);
TFileName fileName;
TParse parse;
parse.SetNoWild( iAvkonAppUi->Application()->AppFullName(), NULL, NULL);
#if defined(__WINS__) || defined(__WINSCW__)
_LIT( KDriveLetter, "c:" );
fileName.Copy( KDriveLetter );
#else
fileName.Copy( parse.Drive() );
#endif
TFileName privatePath;
fs.PrivatePath( privatePath );
fileName.Append( privatePath );
fileName.Append( KSortDataInputFileName );
RFile file;
User::LeaveIfError(file.Open(fs, fileName, EFileStreamText | EFileRead));
CleanupClosePushL(fs);
TInt sz = 0;
User::LeaveIfError(file.Size(sz));
HBufC8* buf = HBufC8::NewLC(sz);
TPtr8 ptr = buf->Des();
User::LeaveIfError(file.Read(ptr));
// Extract numbers from the descriptor containing the contents of the input file
TLex8 lx(ptr);
TBool finshed = EFalse;
while (!finshed)
{
if (lx.Eos())
{
finshed = ETrue;
}
else if ((lx.Peek()).IsDigit())
{
TInt num;
TInt err = lx.Val(num);
iNumbersArray.Append(num);
}
else
{
lx.Inc();
}
}
// automatically closes fs and file
CleanupStack::PopAndDestroy(3);
}