Results 1 to 6 of 6

Thread: Some questions about using notepad file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    69

    Question Some questions about using notepad file

    Hi everybody,

    1- Suppose I have a notepad, and I want to know what is the number of the line which contains the word "Test"... how can I do that?

    and what if more than line contain the same word? How can I show no. of all these lines??

    2- Can I copy the text in the line no. (20) of the notepad to a text or label?!

    Thanks,

  2. #2
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Exclamation Re: Some questions about using notepad file

    Sorry XShadow, you completly misunderstand file I/O.

    If you have no idea how to read and write to files then look it up (or google it)!

    Computers do not work in line numbers, they work on position. for example:

    Code:
    open test.txt for binary as ifile
    ' <- The programs is at line 1 here
    get blah,,ifile
    ' <- After the get statement the program is at line 2
    close ifile
    To answer your question rather than trying to read each line and check if it is "Test" you could instead read the whole file into an array with ease and then check against that. you could pretend the array index is the line number if you want
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Some questions about using notepad file

    This answers both of your questions:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim intFF As Integer, lngN As Long, lngCount As Long, strLines() As String
    
        'change these... or not
        Dim strFileName As String: strFileName = "c:\test.txt"
        Dim strSearchedWord As String: strSearchedWord = "Test"
        Dim blnIgnoreCase As Boolean: blnIgnoreCase = True
        Dim lngLineToCopy As String: lngLineToCopy = 20
    
        'searching for the word...
        intFF = FreeFile
    
        Open Trim(strFileName) For Input As #intFF
            strLines() = Split(Input(LOF(intFF), #intFF), vbCrLf)
        Close #intFF
    
        Debug.Print "Lines from '" & Trim(strFileName) & "' that contains word '" & strSearchedWord & "':"
    
        For lngN = LBound(strLines) To UBound(strLines)
            If blnIgnoreCase Then
                If InStr(1, LCase(strLines(lngN)), LCase(strSearchedWord)) Then
                    Debug.Print (lngN + 1): lngCount = lngCount + 1
                End If
            Else
                If InStr(1, strLines(lngN), strSearchedWord) Then
                    Debug.Print (lngN + 1): lngCount = lngCount + 1
                End If
            End If
        Next lngN
    
        If lngCount = 0 Then
            Debug.Print "None!"
        End If
    
        'displaying the line...
        Label1.Caption = strLines((lngLineToCopy - 1))
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    69

    Re: Some questions about using notepad file

    If you have no idea how to read and write to files then look it up (or google it)!
    Computers do not work in line numbers, they work on position. for example:

    I read many topics about reading text from notepad file... but all of these use the code instr to find a word...

    suppose that i found the word and I want to copy the whole line that contains this word into a label... how can I do that?!


    gavio... thanks... i will try it

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Some questions about using notepad file

    Quote Originally Posted by singularis
    Sorry XShadow, you completly misunderstand file I/O.

    If you have no idea how to read and write to files then look it up (or google it)!

    Computers do not work in line numbers, they work on position. for example:

    Code:
    open test.txt for binary as ifile
    ' <- The programs is at line 1 here
    get blah,,ifile
    ' <- After the get statement the program is at line 2
    close ifile
    To answer your question rather than trying to read each line and check if it is "Test" you could instead read the whole file into an array with ease and then check against that. you could pretend the array index is the line number if you want
    You can read a file line-by-line, using a loop till end-of-file and reading a line using Line Input filenumber, string into which you want it to read
    Show Appreciation. Rate Posts.

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Some questions about using notepad file

    Quote Originally Posted by XShadow
    Computers do not work in line numbers, they work on position. for example:

    I read many topics about reading text from notepad file... but all of these use the code instr to find a word...

    suppose that i found the word and I want to copy the whole line that contains this word into a label... how can I do that?!


    gavio... thanks... i will try it
    In the gavio's example, when you get the word, just do:
    Code:
    Label1.Caption = strLines(lngN)
    
    'or if you want to append to current caption
    
    Label1.Caption = Label1.Caption & "  " & strLines(lngN)
    Show Appreciation. Rate Posts.

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