分享

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

 wstone 2007-11-30
从文件中读n个取数字到RArray中

// 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);  

  }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多