Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading variables from input file 1

Status
Not open for further replies.

VbLearner

Instructor
Apr 4, 2001
4
0
0
US
Dear Colleagues,
I am a new VB learner. I am frostrated with the following question. I am trying to read a file that has several lines of the following data:

1 6 2000 12 05 AM 4.99 4.58 2.85

I created the following small program to achieve that:

Dim p As String * 2
Dim trash As String * 200
Dim date2 As Single
Dim dy, mt, yr, hr1, mn, d1, d2, d3, d4 As Single

Open "c:\work\Eff-Rain\test-00.txt" For Input As #1
Open "C:\Work\Eff-Rain\test-00.dat" For Output As #2

Line Input #1, trash 'this is to read the title line
Do Until EOF(1)

Input #1, dy, mt, yr, hr1, mn, p, d1, d2, d3
If p = "AM" Then
Else
hr1 = hr1 + 12
End If
date2 = 152 + dy - 1 + (hr1 + mn / 60) / 24
d1 = d1 * 0.01
d2 = d2 * 0.01
d3 = d3 * 0.01
Print #2, date2, dy, mt, yr, hr1, mn, p, d1, d2,d3
Loop
lbldone.Caption = "Data processing is done"
End Sub

This program works when I trim the input line to the following:
Input #1, dy, mt, yr, hr1, mn, p
The output is the way I want; but when I try to include the other 3 variables d1, d2 and d3 It tells me that there is a type mismatch. I checked it several time and I spend several hours to try to understand what is going gone; but I could not. Please help me solve this basic question. Thanks.

a VBLearner
 
hey,
try converting them by using type csting i.e cstr(varaiable) of cdate(...) o.k
if this is not sufficient i will give you the detail coding but don't be lazy o.k
srusti
 
Srusti,
I did not understand your input. Could you please explain it more. Please keep in mind that I am a new starter in VB. Thanks.

VBLearner.
 
VBLearner,
I think your problem is stemming from the fact that the line
Code:
Dim dy, mt, yr, hr1, mn, d1, d2, d3, d4 As Single
does not make each one of these variables a Single. Everything except d4 is a Variant data type. In VB you can't define a bunch of variable on one line and assume that they are all assigned that single data type. It should look like this instead:
Code:
Dim dy, mt, yr, hr1, mn As Variant
Dim d1 as Single 
Dim d2 as Single 
Dim d3 as single 
Dim d4 as Single
I'm leaving some variables as variants because you said that part of the fileline works as is so theres no reason to change them. Remember that unless you have an 'As Whatever' IMMEADIATELY after each variable definition the variables are treated as variant. This is a common mistake because the way you had it written would seem to make sense, but it doesn't work that way. Without knowing that d1 - d3 were singles VB probably treated them as text and saw the '.' character as a field seperator. I think it will work with only these changes.
Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Ruairi
Thank you for the tip. I did not think of that since it is allowed in Fortran and Pascal. After making that change I run into another problem:
"Run Time Error '62', input past End of File
I change the Do until EOF(1) to Do While NOT EOF(1) but still I have the same problem. In stead I used a counter and Do While counter <= . If I have let say 10 lines of data in input file (N = 10). If I read the first 8 lines of data I will not have the Error but If I try to read 9 or the whole 10 lines of data I will have the same problem. I tried it with different data lines and all the time the same response I get. It only allow N-2 lines of the input data to be read without an error message.
Why is this!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top