2015-01-15 4 views
-1

Я пытаюсь выяснить, как я могу выбрать определенные строки из открытого txt-файла в VB6. Мне уже удалось открыть файл и присвоить его переменной (не уверен, что это сработает). Файл txt, который был открыт, также содержит другую информацию, которая не имеет отношения к делу, поэтому необходимо создать новый файл с соответствующей информацией. я вроде написал какое-то алгоритм, это не очень хорошо, хотя: PВыбор некоторых строк из txt-файла, VB6

'READ Line 01 
'REPEAT 
    'IF line begins with "studentname=" then 
     'Copy to new textbox, in new textbox/variable create new line 
    'ELSE If line begins with "studentID=" 
     'Copy to new textbox, in new textbox/variable create 2 new lines 
    'ENDIF 
    'READ next line 
'UNTIL end of text is reached 

Кто-нибудь знает, как я могу добиться этого? Благодаря

+1

Пожалуйста, найдите время, чтобы проверить это * тщательно *: [Задать] – Plutonix

ответ

3

Это пример кода для Вашего вопроса:

Filename$="myfilename.txt"  'This row assigns to variable Filename$ 
           'the name of your file, example: 
           '"myfilename.txt" 

Open Filename$ For Input As #1 'This row opens the file Filename$ 

    Do While Not EOF(1)   'This row inits the loop to read all 
           'lines in your file and loop ends when 
           'there is not others lines to read 

    Line Input #1, line$   'This row inserts on variable line$ a 
           'line of your file 

    If mid(line$,1,12)="studentname=" then 'In this row the command mid 
              ' extracts the first 12 chars 
              'of line and verify if is 
              'equal to "studentname=" 

    Text1.Text=mid(line$,13)  'Is equal? Then In this line mid 
           'extracts from 13° char 
           'to last char and set it on Text1 

    ElseIf mid(line$,1,10)="studentID=" then 'Is not equal to 
              ' "studentname="? Then 
              ' verify if the first 10 
              ' chars are equals to 
              ' "studentID=" 

    Text2.Text=mid(line$,11)  'Is equal? Then In this line mid 
           'extracts from 11° char 
           'to last char and set it on Text2 

    '... You can insert others ElseIf conditions 

    End If       

    Loop      'Loop 

Close #1      'This row closes file 
+0

Спасибо за ответ, не могли бы вы объяснить, что каждая часть делает, и как я могу включить его для работы с моим кодом? Еще раз спасибо –

+0

Я отредактировал ответ, я надеюсь, что essert полезно. – Mailkov

+0

Спасибо большое, это было очень полезно –

Смежные вопросы