CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    54

    how to read text file from bottom going up (reverse)..

    the start of every log is the time stamp enclosed in "[" and "]"

    i wanted to read the last log based on the sample below where it should be:

    [1/26/12 1:06:11:621 CST] 0000003b ExceptionUtil E CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "getObjects" on bean "BeanId

    this is the sample code i use to read the log file:

    Code:
    TextReader tr = new StreamReader("c:\log.txt");
    if i use: string endText = tr.ReadToEnd(), this will give me the last line which is:

    at com.filenet.engine.ejb.EngineCoreBean.getUserName(EngineCoreBean.java:744)


    how can i get to read the start of this last log from bottom up?

    thanks in advance.


    Code:
    [1/25/12 1:06:11:652 CST] 0000003b LocalTranCoor E   WLTC0017E: Resources rolled back due to setRollbackOnly() being called.
    [1/25/12 1:06:33:027 CST] 00000037 WSRdbManagedC W   DSRA9510W: Custom property preTestSQLString should not be empty if the pre-test connection option is selected.
    [1/25/12 1:14:41:159 CST] 00000025 LdapRegistryI A   SECJ0419I: The user registry is currently connected to the LDAP server ldap://SANETDCPRD01:389.
    [1/26/12 1:06:11:621 CST] 0000003b LocalTranCoor E   WLTC0017E: Resources rolled back due to setRollbackOnly() being called.
    [1/26/12 1:06:11:621 CST] 0000003b ExceptionUtil E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "getObjects" on bean "BeanId(FileNetEngine#Engine-ejb-ws.jar#Engine, null)". Exception data: com.filenet.api.exception.EngineRuntimeException: SECURITY_LDAP_PROVIDER_FAILED: LDAP Provider Access failed for principal 'P8PEAdmin_DEV'. Root cause: SANETDCPRD01:3268; socket closed
                    at com.filenet.engine.security.SecurityProvider.getUser(SecurityProvider.java:681)
                    at com.filenet.engine.jca.impl.RequestBrokerImpl.getUserName(RequestBrokerImpl.java:1291)
                    at com.filenet.engine.ejb.EngineCoreBean._getUserName(EngineCoreBean.java:762)
                    at com.filenet.engine.ejb.EngineCoreBean.getUserName(EngineCoreBean.java:744)

  2. #2
    Join Date
    Apr 2011
    Posts
    54

    Re: how to read text file from bottom going up (reverse)..

    would it help if i know the total number of lines in the log file?
    say i have (using the example), 9 lines.

    can i read this from 9th line, then 8th, then 7th... then when I find the start of the log i stop there? how can i go doing it to read like this? (if there's a way)..

    thanks

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: how to read text file from bottom going up (reverse)..

    If the file was one with fixed length records then it would be simple as you would know the byte postion of each record based on recordlength and record number. You would know the number of records based on record length and size of file but in the case of variable length records there is no way to tell without having a header that would tell you where each record starts.

    You could read the entire file into memory then split() the file into an array and then read the array down from the highest index to 0.
    Last edited by DataMiser; February 13th, 2012 at 01:40 PM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Apr 2011
    Posts
    54

    Re: how to read text file from bottom going up (reverse)..

    it's not a fixed length.. been searching for a better solution but all points (or suggests) the same approach as you've mentioned here.. appreciate the input and thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured