Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how can i read the text file?

Posted on 2000-03-26
24
Medium Priority
?
160 Views
Last Modified: 2010-05-02
i need to open the text file with the following content and read each field one by one. (assume file name ="log.txt")

Event  Date  Time  User#  User_Name
__________________________________
                                                           
IN  02/21/00 12:54   11    Thomas
IN  02/21/00 13:04   23    Gio
OUT 02/21/00 13:05   36    Mich
OUT 02/21/00 13:08   28    Ereno
OUT 02/21/00 13:10   19    Nocot
IN  02/21/00 13:10   2     Rrez


How can I read the file line by line, and get the fields one by one, because i need to insert the fields into db.
0
Comment
Question by:binary1
  • 8
  • 6
  • 5
  • +2
24 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 100 total points
ID: 2659851
Dim intFile As Integer
Dim strLineRead As Integer
Dim intPos As Integer
Dim intLastPos As Integer
Dim strINOUT As String
Dim strDate As String
Dim strUserNumber As String
Dim strUserName As String

intFile = FreeFile

Open "X:\Y\Z\log.txt" for Input As #intFile

Do While Not EOF(intFile)
    Line Input #intFile, strLineRead
    intPos = Instr(strLineRead, " ")
    strINOUT = Left(strLineRead, intPos - 1)

    intLastPos = intPos + 1
    intPos = Instr(intLastPos, strLineRead, " ")
    strDate = Mid(strLineRead, intLastPos, intPos - intLastPos)

    intLastPos = intPos + 1
    intPos = Instr(intLastPos, strLineRead, " ")
    strUserNumber = Mid(strLineRead, intLastPos, intPos - intLastPos)

    strUserName = Right(strLineRead, len(strLineRead) - intPos)
Loop

Close #intFile
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2659876
binary1,

Please do not touch this question for the moment. As you can see there is a problem with this question: I managed to post TWO proposed answers. I'd like to give Community Support a chance to look at this, so would you mind not rejecting or accepting any of the above proposed answers for the moment?
0
 
LVL 3

Expert Comment

by:darinw
ID: 2659879
Reviewing question.

darinw
Customer Service
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:binary1
ID: 2659983
I think there must be some thing wrong with the code, because i can't i can't get any value for strinout,strdate, and etc...most likely it was cause by the leading space in each line, if u can solve this proble for me, i will give the score and a for me. thanks ...sorry i am new in basic
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2660011
Hi binary1,

If your lines contain leading spaces, replace the first Instr line
    intPos = Instr(strLineRead, " ")

with something like
    intPos = Instr(N, strLineRead, " ")

where N is the first non-blank position in each line.

This only works if each line starts with the same number of spaces, or if you can guarantee that a certain position at the start of the line is not blank, e.g.

  A
 BA
  C
  D
 EF
GHE
WOO
HOO
 AB

In the list above you know that position 3 is always filled, so use 3 as the value of N.


Hope this helps.

Again, please do not accept or reject the answer until Customer Service have had a chance to look into the double proposed answer posting, thanks ;-)
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2660017
Oh, PS, in that case, strINOUT would be filled as follows:

    strINOUT = Trim(Left(strLineRead, intPos - 1))

to get rid of leading blanks.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2660131
Caraf_g:
If he has VB6 this can be a whole lot neater.. <smile>. Modifying your loop..

<----- Code Begin ----->

Dim strInput() as String
Do While Not EOF(intFile)
    Line Input #intFile, strLineRead
    strInput() = Split(strLineRead, " ")
    strInout = strInput(0)
    strDate = strInput(1)
    strUserNumber = strInput(2)
    strUserName = strInput(3)
Loop

<----- Code End ----->

Don't ya just love a nosey kibitzer?.. <LOL>

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2660146
Caraf_g:
Oh.. and to clean up leading and trailing spaces put this after the LineInput statement..

  strLineRead = Trim(strLineRead)  
0
 
LVL 28

Expert Comment

by:Ark
ID: 2660177
Hi
Do you see that there are TWO spaces before last field? :-)
Cheers
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2660618
kewl. Good observation!


Dim intFile As Integer
Dim strLineRead As Integer
Dim intPos As Integer
Dim intLastPos As Integer
Dim strINOUT As String
Dim strDate As String
Dim strUserNumber As String
Dim strUserName As String
Dim intStart As Integer

intFile = FreeFile

Open "X:\Y\Z\log.txt" for Input As #intFile

intStart = <some appropriate value. intStart is the value N I was talking about in an earlier comment>
Do While Not EOF(intFile)
    Line Input #intFile, strLineRead
    intPos = Instr(intStart, strLineRead, " ")
    strINOUT = Trim(Left(strLineRead, intPos - 1))

    intLastPos = intPos + 1
    Do While intLastPos <= len(strLineRead)
        If Mid(strLineRead, intLastPos, 1) = " " Then
            intLastPos = intLastPos + 1
        Else
            Exit Do
        End If
    Loop
    intPos = Instr(intLastPos, strLineRead, " ")
    strDate = Mid(strLineRead, intLastPos, intPos - intLastPos)

    intLastPos = intPos + 1
    Do While intLastPos <= len(strLineRead)
        If Mid(strLineRead, intLastPos, 1) = " " Then
            intLastPos = intLastPos + 1
        Else
            Exit Do
        End If
    Loop
    intPos = Instr(intLastPos, strLineRead, " ")
    strUserNumber = Mid(strLineRead, intLastPos, intPos - intLastPos)

    strUserName = Right(strLineRead, len(strLineRead) - intPos)
Loop

Close #intFile


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2660635
Couplatrims for good measure:

.....
    strDate = Trim(Mid(strLineRead, intLastPos, intPos - intLastPos)

.....
    strUserNumber = Trim(Mid(strLineRead, intLastPos, intPos - intLastPos))

..... and
    strUserName = Trim(Right(strLineRead, len(strLineRead) - intPos))
0
 

Author Comment

by:binary1
ID: 2662491
Thanks, can i accept u answer right now? However i find the better solution by using split and check with the condition....I'll give a to u for u kindness
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2662943
Assuming you are completely satisfied, you can select the comment you feel most appropriate to your need and accept it as the answer.. <smile>.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2662948
No offense to Caraf_g.. but if you prefer my comment (the Split).. then reject Caraf_Gs answer.. and then go back in and accept Wsh2's.. <smile>.
0
 

Author Comment

by:binary1
ID: 2662980
wsh2
  u solution also have the same problem with the space in between the line, but most likely i will split the score for both of u, btw how can i split the score?
0
 
LVL 28

Expert Comment

by:Ark
ID: 2663009
binary1,
Split is better. First, you can remove from array all empty (ie filling with spaces) entries, so you can read files with ANY length spaces delimiters. Second, it's easy to change code for any other delimiter (commas,tabs etc). The only problem - this code require VB6 :-(
Cheers
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2663149
To split the score post a question in Community Support (see link at very end of page). They'll reduce the number of points for this question by half, and you can then give it to one person. For the other person you can post a dummy question "points for TheOtherPerson" for the other half of the points, and they can then answer that question instead. When you post such a dummy question always mention the URL of the original question so that other people will know why you're doing it.

Hope this helps!

Pino
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2663151
To split the score post a question in Community Support (see link at very end of page). They'll reduce the number of points for this question by half, and you can then give it to one person. For the other person you can post a dummy question "points for TheOtherPerson" for the other half of the points, and they can then answer that question instead. When you post such a dummy question always mention the URL of the original question so that other people will know why you're doing it.

In the CS question also mention this URL. You don't need to assign points to it.

Hope this helps!

Pino
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2663157
binary1 writes: "space in between the line"

If you mean extra space between the fields.. easy..

Do While Not EOF(intFile)
  Line Input #intFile, strLineRead
  strLineRead = Trim(strLineRead)
  Do While instr(1, strLineRead, "  ") <> 0 _
    strLineRead = Replace (strLineRead, "  ", " ")
  Loop

<smile>
---------------------------------------
To award points.. to more than one person.. take out a new question and in the subject place FOR <EE name>.. (eg. in my case it would be FOR WSH2).. and assign the points.. <smile>.

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2663164
By the way Binary1.. as to points.. have you seen Tseyeh recently?.. <smile>.
0
 

Author Comment

by:binary1
ID: 2667503
yup... he is my officemate
0
 

Author Comment

by:binary1
ID: 2692012
why they don't reduce the score to half?
0
 
LVL 3

Expert Comment

by:darinw
ID: 2698751
Community Support has reduced points from 50 to 25
0
 
LVL 3

Expert Comment

by:darinw
ID: 2698752
Reducing points to allow split.

Sorry for the delay. For some reason, your Community Support question was not visible until today :(

darinw
Customer Service
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question