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

Read each line of the text file with this code


here is my code:
this reads the first line of a text file and puts each char in an array (tableau).
I would like to do that but for each line of the text file  (using fso).
(this is gonna overwrite the data in the array, i know..thats what i want)


    Set textStreamObject = fso.OpenTextFile("data.tmp", 1, False, 0)
    tmp = textStreamObject.ReadLine
    ReDim tableau(1 To Len(tmp))
    i = 1
    Do While i <= Len(tmp)
        tableau(i) = Mid$(tmp, i, 1)
        i = i + 1
    Loop
    Wend
0
Punker51
Asked:
Punker51
  • 6
  • 4
1 Solution
 
winner21Commented:
The only thing you need is to make a recursion for your code, same as the operation for line 1. Please add the following code as your outer loop:


Dim strIn as string  ' the variable for holding the whole line stuff.

Open "data.tmp" For Input As #1
 
Do while not EOF(1)
   Line Input #1, strIn
   tmp=len(strin)
   ReDim tableau(1 To Len(tmp))
   i = 1
   Do While i <= Len(tmp)
       tableau(i) = Mid$(tmp, i, 1)
       i = i + 1
   Loop
   Wend
  Close #1
Loop
0
 
Punker51Author Commented:
To winner21: when i try this i get this eror: bad file name or number (on the do while not EOF() line)
0
 
winner21Commented:
You must specify the path to the file name. If the data.tmp is under C:, the first line you should change to "C:\data.tmp".
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Punker51Author Commented:
Winner21: i did
0
 
winner21Commented:
by the way, if the file is a text file, you cannot use tmp as the extension name. This is the reason you get the error. By the way, there is a mistake in my code, the variable tmp I declared is for holding the length, please discard this variable, and replace all tmp by strln, or you can simply replace strln by tmp.

0
 
winner21Commented:
this is because your file is empty or something else. PLease check your file and make sure there is somthing there.
0
 
winner21Commented:
let me write this code for you quickly as i have to go home now.
0
 
winner21Commented:
I have a text file under C: root and it contains the following info.

sdsdsdsd
hrgrgrfsd;'
jkyhjtyh

*********************

Code:

Private Sub Command1_Click()

Dim tmp

Open "c:\data.txt" For Input As #1

Do While Not EOF(1)
  Line Input #1, tmp
 
  ReDim tableau(1 To Len(tmp))
  i = 1
  Do While i <= Len(tmp)
      tableau(i) = Mid$(tmp, i, 1)
      Debug.Print tableau(i)
      i = i + 1
  Loop
 
Loop
Close #1
End Sub


Result:

s
d
s
d
s
d
s
d
h
r
g
r
g
r
f
s
d
;
'
j
k
y
h
j
t
y
h


****************************

Is this your want?


0
 
Punker51Author Commented:
exactly thanx a lot m8
0
 
Punker51Author Commented:
very fast replying
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now