CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Question recognize first three characters in a line?

    dear all,

    i have a sticky problem. i have some files where, among others, an unknown number of lines starts with "Jxyz" followed with a line starting with "end"; the Jxyz lines are all followed and contain text numbers and commas, randoomly ordered.

    I need to perform a certain action on the Jxyz lines and stop the "cycle" when the "end" line is reached.

    There are other lines before and after this block of lines, so EOF if not an option.

    My problem is: how to "recognize" the "end" line so that I can stop the cycle and go on with the program?

    Any help would be marvellous.

    Cheers,

    GJ

  2. #2
    Join Date
    Nov 2005
    Posts
    36

    Re: recognize first three characters in a line?

    So is this a text file, a database, etc... you need to be a little more specific there.

    Also how are you reading this file??

    You can always use
    Code:
    if left$(howeveryourdoingthis, 4) = "Jxyz" then
           Do what you need to
    end if

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: recognize first three characters in a line?

    I would loop through the code checking for the "end" string, because this is obviouly a defined sign
    Code:
    ' open your file here
    Do
    	 'here you need to read in the line line by line into 'sLineString'
    	 LineInput #1, sLineString
    	 if  left(sLineString,3) = "end" then exit do
    loop
    If you tell us what sort of file ( text, doc, xls,database ? )are u using we maybe can help you more. Hope that helps
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: recognize first three characters in a line?

    Or, read it all into a buffer, and work from there. It would be faster.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim x As Integer, st As String
      Dim ff As Integer
      Dim strBuff As String
      Dim str() As String
      ff = FreeFile
      Open App.Path & "\to do.txt" For Input As #ff
        strBuff = Input(LOF(ff), ff)
      Close #ff
      ' ----------------- two ways to skin a cat --------------
      MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
      ' -------------------------------------------------------
      str() = Split(strBuff, vbCrLf)
      MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      For x = 0 To UBound(str)
    ' check for "END" and if so, then Exit For to get out of the loop
        st = st & str(x) & vbCrLf & vbCrLf
      Next x
      MsgBox st
    End Sub
    There are two ways to count number of lines, as you can see.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jan 2006
    Posts
    2

    Thumbs up Re: recognize first three characters in a line?

    Actually, I took the idea of JonnyPoet and worked on it and it worked just the way I wanted.

    Thanks u all,
    GJ
    Last edited by gjustino; January 26th, 2006 at 04:44 PM.

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: recognize first three characters in a line?

    Quote Originally Posted by gjustino
    Actually, I took the idea of JonnyPoet and worked on it and it worked just the way I wanted.

    Thanks u all,
    GJ
    You are welcome. Please dont forget ( if not done in between )to sign this post as resolved ( Use the Threadtools in the upper line, so others didn't spend any more attention in this request
    If you need further help always feel free to ask.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

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