reading strings from a text file 
Author Message
 reading strings from a text file

I posted a note a while ago asking for some help with reading a txt file,
capturing variables and then writting them to a database table.

The suggestion was to use a while not at EOF routine that used lineinput to
grab the variables. I had no problems with the post processing, and thus it
worked well. (Thanks to all those that helped out). This was for variables
that were within the same line.

The problem is now that I need to read in some variables that are long text
strings that may go over a number of lines. eg
Variable1:blah blah blah blah
blah blah blah blah
Variable2:more blah blah

I need to be able to read the ":" at the end of the 2nd variable name and
thus have the first variable captured. I know the order that the variable
names will appear, and that they will always have a ":" to end the variable
name, but not the number of lines that they will take up.

Any hints would be great.

Thanks
--
Michael Whitaker
WAIS



Sat, 05 Aug 2000 03:00:00 GMT  
 reading strings from a text file

Michael Whitaker a crit dans le message

Quote:
>I need to be able to read the ":" at the end of the 2nd variable name and
>thus have the first variable captured. I know the order that the variable
>names will appear, and that they will always have a ":" to end the variable
>name, but not the number of lines that they will take up.

I'm not sure I totally understand the problem but wouldn't something like
this do it?  If not... where exactly is the problem?

if (right(strInputLine, 1) = ":") then
    ' at the end of the variable
end if

-----
Please reply via EMail *AND* Newsgroup if possible.


Paris, FRANCE                      Web: http://www.erb.com



Sun, 06 Aug 2000 03:00:00 GMT  
 reading strings from a text file

It's not as difficult as you think.  I did something similar recently.

-------------------------------------------------------------------
Dim CurrentLine, Variable1, Variable2 as String
Dim WorkingOn, PrevWorkingOn as byte

PrevWorkingOn = 0
Open "InputFile.txt" for input as #1
While Not EOF(1)
    WorkingOn = 0
    Line Input #1, CurrentLine
    If Left(CurrentLine,10) = "Variable1:" then WorkingOn = 1
    If Left(CurrentLine,10) = "Variable2:" then WorkingOn = 2
    If WorkingOn = 0 then   'Continued variable
        Select Case PrevWorkingOn
            Case 0:    <Skip This Line or Handle Error - this will only occur
                              if the first line in the text file does not have
a variable: prefix.>
            Case 1:    Variable1 = Variable1 & CurrentLine
            Case 2:    Variable2 = Variable2 & CurrentLine
        End Select
    Else    ' New Variable
        Select Case WorkingOn
            Case 1:    If Variable1 <> "" Then <Process or Output Variables>
                            Variable1 = Mid(CurrentLine,11)
            Case 2:    Variable2 = Mid(CurrentLine,11)
        End Select
    End If
    PrevWorkingOn = WorkingOn
Wend
-------------------------------------------------------------------
This is a simple example, with only 2 variables.  Obviously you will
want to change it to accomodate more.  If you have too many more
variables, you may want to use Variable(n) rather than Variable1,
Variable2, etc...

Hope this helps.
-Doug.

Quote:

> I posted a note a while ago asking for some help with reading a txt file,
> capturing variables and then writting them to a database table.

> The suggestion was to use a while not at EOF routine that used lineinput to
> grab the variables. I had no problems with the post processing, and thus it
> worked well. (Thanks to all those that helped out). This was for variables
> that were within the same line.

> The problem is now that I need to read in some variables that are long text
> strings that may go over a number of lines. eg
> Variable1:blah blah blah blah
> blah blah blah blah
> Variable2:more blah blah

> I need to be able to read the ":" at the end of the 2nd variable name and
> thus have the first variable captured. I know the order that the variable
> names will appear, and that they will always have a ":" to end the variable
> name, but not the number of lines that they will take up.

> Any hints would be great.

> Thanks
> --
> Michael Whitaker
> WAIS



Sun, 06 Aug 2000 03:00:00 GMT  
 reading strings from a text file

Michael

If you know how many variables you are going to receive try something like
the following

For intCnt =1 to NoOfVariables
     Select Case intCnt
           Case 1
                strVar = "Variable1 Name"
        Case 2
                strVar = Variable2 Name"
            Case 3
                etc

      End Select

      If strVar = Left(LinePutVariable), Len(strVar) Then
           You have found next variable

I'm sure that this will give you enough to figure out the rest :-)

Hope this helps

Terry



Quote:
> I posted a note a while ago asking for some help with reading a txt file,
> capturing variables and then writting them to a database table.

> The suggestion was to use a while not at EOF routine that used lineinput
to
> grab the variables. I had no problems with the post processing, and thus
it
> worked well. (Thanks to all those that helped out). This was for
variables
> that were within the same line.

> The problem is now that I need to read in some variables that are long
text
> strings that may go over a number of lines. eg
> Variable1:blah blah blah blah
> blah blah blah blah
> Variable2:more blah blah

> I need to be able to read the ":" at the end of the 2nd variable name and
> thus have the first variable captured. I know the order that the variable
> names will appear, and that they will always have a ":" to end the
variable
> name, but not the number of lines that they will take up.

> Any hints would be great.

> Thanks
> --
> Michael Whitaker
> WAIS



Sun, 06 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ** File I/O ** Need help reading text file into String variable

2. VB6 : How to read partial string within a text file

3. reading tab delimited strings from a text file

4. Read from Specific String and write to Text File

5. Read text file line into string in vb dll

6. Evaluating strings read from a text file

7. Read text file to string

8. Reading from a text file and writing toa text file from Vis Bas 6.0

9. Help parse a text string from sequential text file

10. Search for a text string in text file

11. How to replace text string in text file

12. How to read partial text in a line of a text file

 

 
Powered by phpBB® Forum Software