• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Reading text file

I have a text file. What I want to do is to read every 4th line in that file and store it in a variable. Please tell me how I can do this using VB?

Thanks in advance
RR
0
r_nair
Asked:
r_nair
  • 7
  • 3
  • 2
  • +1
1 Solution
 
crazymanCommented:
dim intFile as freefile
dim strtemp as string
dim x as integer
open "c:\whatever.txt" for input as #intfile
do while not eof(intfile)
    x=x+1
   line input #intfile,strTemp
   if x = 4 then
       msgbox strtemp
       x=0
   end if
loop
close #intfile
0
 
crazymanCommented:
the message box will be each 4th line of text
0
 
crazymanCommented:
Forgot

IntFile=FreeFile

before the open statement
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
danlevansCommented:
r_nair

Private Sub Command1_Click()
  Dim intfile As String
  Dim l As String
  Dim ctr As Integer
  Dim urvariable As String
  ctr = 1
  intfile = FreeFile
  Open "c:\urfile" For Input As #intfile
  Do Until EOF(intfile)
    Line Input #intfile, l
    If ctr = 4 Then
        urvariable = l
        ctr = 1
    Else
        ctr = ctr + 1
    End If
  Loop
  Close #intfile
End Sub
0
 
r_nairAuthor Commented:
Hi
It gives me an error "User defined type
not defined" at the line Dim Intfile As freefile


Thanks
RR
0
 
BarryTiceCommented:
r_nair: This loads your lines into the strData() array. I warn you that it is not what a lot of people would consider best programming, though, because it leaves strData(0) empty. Other than that, it should do the trick for you. -- b.r.t.

dim intFile as Integer
dim strData() as String
dim intCount as Integer
dim intCount2 as Integer

intFile = FreeFile
intCount = 1
intCount2 = 1
redim strData(1)

Open "C:\FileName.txt" for input as #intFile
Do While Not EOF(intFile)
    Line Input #intFile, strData(intCount)
    intCount2 = intCount2 + 1
    If intCount2 Mod 4 = 0 Then
        intCount = intCount + 1
        Redim Preserve strData(intCount)
    End If
Loop
Close #intFile

redim Preserve strData(intCount - 1)


0
 
crazymanCommented:
Dim intfile as integer
intfile=freefile
0
 
crazymanCommented:
It was a typo
it is an integer
0
 
BarryTiceCommented:
r_nair

Crazyman meant to say:

Dim intFile as Integer
intFile = FreeFile
0
 
BarryTiceCommented:
Sorry Crazyman. You beat me to the correction.
0
 
crazymanCommented:
DanLevans you have dimmed

>>dim IntFile as string <--typo?
0
 
danlevansCommented:
oops I was trying to get ahead of you guys :-)
0
 
crazymanCommented:
LOL :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now