2013-03-11 1 views
0

선생님은 수업을 통해 파일에서 저자 이름을 검색하고 해당 작성자의 모든 정보를 우편물 레이블과 같은 텍스트 상자에 표시하려고합니다.Visual Basic에서 텍스트 파일을 검색하는 방법

다음은 아래 코드를 추가 한 코드입니다. 필자는 Authors 문자열을 가져 와서 다른 파일을 검색하는 프로그램을 얻는 것에 대해 정말로 분실했다. 필자는 저자 이름을 완벽하게 표시 할 수 있지만 그 이름에 대해 다른 파일을 검색하고 작성자에 대한 해당 줄에 정보를 표시하는 방법은 무엇입니까?

Imports System.IO 

Public Class Form1 
' CSCI 6 
' Alex Smutny 
' Lab #3 
' 
' DATE: 3/1/2013 

Dim SR As IO.StreamReader 

' initial start spot for the record Count 
Dim RecordCount As Integer = 0 
Dim Author As String = File.ReadAllText("Authors.txt") 



Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitBtn.Click 
    ' Properly Exits the application. 

    Me.Close() 
End Sub 

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeBtn.Click 
    readBtn.Text = "Read" 
    SR.Close() 

    ' Changes the button statuses Locked or unlocked and sets default settings. 
    openBtn.Enabled = True 
    quitBtn.Enabled = True 
    readBtn.Enabled = False 
    closeBtn.Enabled = False 
    SearBtn.Enabled = False 

    RecordCount = 0 

    recordNum.Clear() 
    bookNum.Clear() 
    isbnNum.Clear() 
    bookTitle.Clear() 
    authorName.Clear() 
    bookPrice.Clear() 
    bookQuanity.Clear() 
    reorderPoint.Clear() 

End Sub 

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openBtn.Click 
    ' Sets the file to read from and opens it. 
    SR = IO.File.OpenText("Books.Txt") 

    ' Changes the status of the buttons again now that the File is open and ready to read. 

    openBtn.Enabled = False 
    quitBtn.Enabled = False 
    readBtn.Enabled = True 
    closeBtn.Enabled = True 
    SearBtn.Enabled = True 

End Sub 

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readBtn.Click 
    'Changes the Read button to say next because it makes more since. 
    readBtn.Text = "Next" 
    ' Setting all the variables. 
    Dim srIndex1 As Integer = 18 
    Dim srTitle As Integer 
    Dim srAuthor As Integer 
    Dim srPrice As Integer 
    Dim srDPrice As Double 
    Dim srData As String 
    Dim srLength As Integer 


    If SR.Peek() = -1 Then 
     MessageBox.Show("End of file") 
    Else 
     ' Starts to read 
     srData = SR.ReadLine 

     ' Increment counter by 1. 
     RecordCount = RecordCount + 1 

     ' Determine the record length. 
     srLength = srData.Length 

     ' gets the book number 
     bookNum.Text = srData.Substring(0, 3) 

     ' ISBN number 
     isbnNum.Text = srData.Substring(4, 13) 

     ' Book title 
     For srTitle = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srTitle, 1) = "," Then 
       bookTitle.Text = srData.Substring(srIndex1, srTitle) 
       srIndex1 = srIndex1 + srTitle + 1 
       srTitle = srLength + 1 
      End If 
     Next 

     ' Book Author 
     For srAuthor = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srAuthor, 1) = "," Then 
       authorName.Text = srData.Substring(srIndex1, srAuthor) 
       srIndex1 = srIndex1 + srAuthor + 1 
       srAuthor = srLength + 1 
       Author = authorName.Text 
      End If 
     Next 

     ' Book Price 
     For srPrice = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srPrice, 1) = "," Then 
       srDPrice = CDbl(srData.Substring(srIndex1, srPrice)) 
       bookPrice.Text = srDPrice.ToString("C") 
       srIndex1 = srIndex1 + srPrice + 1 
       srPrice = srLength + 1 
      End If 
     Next 

     ' Quanity 
     bookQuanity.Text = srData.Substring(srIndex1, 2) 
     srIndex1 = srIndex1 + 3 

     ' Reorder Point 
     reorderPoint.Text = srData.Substring(srIndex1, 2) 

    End If 

    ' record count 
    recordNum.Text = RecordCount 

End Sub 



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    openBtn.Enabled = True 
    quitBtn.Enabled = True 
    readBtn.Enabled = False 
    closeBtn.Enabled = False 
    SearBtn.Enabled = False 


End Sub 

Private Sub SearBtn_Click(sender As System.Object, e As System.EventArgs) Handles SearBtn.Click 
    SR = IO.File.OpenText("Authors.Txt") 

    SR.Close() 
    readBtn.Text = "Restart" 
    RecordCount = 0 
    SR = IO.File.OpenText("Books.Txt") 
End Sub 

최종 클래스

Program

저자는 http://pastebin.com/y6DNyUFd

그래서 내 목표는 단지 ​​현재의 저자에 대한 정보를 얻을 후 다른 파일을 얻을 수 있습니다 파일 http://pastebin.com/t7C8ye9e 책 파일 뒤에 오는 공백을 모두 꺼내 우편 라벨로 표시하십시오.

답변

0

이렇게하는 데는 여러 가지 좋은 방법이 있습니다. 지금은 한 줄에 한 명의 저자가 있다고 가정 해 보겠습니다. 있는 경우 전체 문자열을 My.Computer.FileSystem.ReadAllText()으로 가져오고 String.Split(Environment.NewLine)을 사용하여 각 줄마다 분할 할 수 있습니다. 어쨌든 목록의 각 저자를 구분할 수있는 방법이 필요합니다. String.Split은 String 객체의 배열을 반환합니다. 그런 다음 배열을 반복하여 용어가 검색과 일치하는지 확인할 수 있습니다. 다음은 for 루프의 예입니다.

궁금한 점이 있으면 알려주세요.

관련 문제