First time text file read/write user...vb6

Keeping text file closed, I want to start with basic steps to buid a routine to read from a text file and later write to it.  I want solid operation and want some info on text file format.  Is data shown below a good option:
Field1, Field2, Field3
1,12,A
2,34,C
3,36,E

Thanks
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brianb99999Commented:
Standard flat file Read and write routines:

Read:
    Dim intFileHandle As Integer
    Dim strRETP As String
    intFileHandle = FreeFile
    Open "path to file" For Input As #intFileHandle
    Do While Not EOF(intFileHandle)
        Line Input #intFileHandle, strRETP
        MsgBox strRETP
    Loop
    Close #intFileHandle

Write (creates a new file overwriting any existing file):
    Dim intFileHandle As Integer
    Dim strRETP As String
    strRETP = "Hi There"
    intFileHandle = FreeFile
    Open "path to file" For Output As #intFileHandle
    Print #intFileHandle, "---------------------------"
    Print #intFileHandle, strRETP
    Print #intFileHandle, "---------------------------"
    Close #intFileHandle

Write (appends data to an existing file):
    Dim intFileHandle As Integer
    Dim strRETP As String
    strRETP = "Hi There"
    intFileHandle = FreeFile
    Open "path to file" For Append As #intFileHandle
    Print #intFileHandle, "---------------------------"
    Print #intFileHandle, strRETP
    Print #intFileHandle, "---------------------------"
    Close #intFileHandle

Brian.
0
wehoitCommented:
A few comments on Brian's answer:

1) You should use "Write" instead of "Print" to output comma separated values. e.g.:
Dim iOne as integer, iTwo as integer, iThree as String
iOne=1
iTwo=12
iThree="A"
...
Write #intFileHandle, iOne,iTwo,iThree
...
Gives this output:
1,12,A

etc.


2) You should then use "Input" not "Line Input" to read the comma separated values:
Dim xOne as integer, xTwo as integer, xThree as String
...
Input #intFileHandle, xOne,xTwo,xThree
...

This reads each separated value into the specified variables...


0
jimbobmcgeeCommented:
You don't have to use Write instead of Print -- if you use Write, it will enclose all text strings in "quote marks" and all dates in #hashes#.  This can be useful, as it eliminates the error that will occur in a CSV if text you want to write contains commas and allows you to put date values straight into a Date variable, but may not be what you want.  Print is a WYSIWYG output system.

Similarly, you don't have to use Input instead of Line Input.  Input will read until it reaches a comma or a new line.  Line Input will read until it reaches a new line only.  You can use Line Input to read the whole line and split this line with thisArray = Split(strLineJustRead, ","), to get comma-separated values in one chunk.

Personally, if I'm writing a CSV-style flat file, I prefer to use a different delimeter, such as pipe (|) -- this prevents the CSV being confused by any commas that may be deliberately in the text, e.g.

    Print #filenum, "this text, here, has a comma|54455|223"

Another thing to note is, when writing multiple data items to the file, if you don't want to insert a new line, follow your Print line with a semicolon (;), e.g.:

    Print #filenum, "I want the text in this string to be on the same line -> ";
    Print #filenum, myString;
    Print #filenum, " But I want a new line now."             'NO ; MOVES TO NEW LINE

The Open/Close method is an older method -- it is often favourable to use the object orientated approach, nowadays.  To do this, add a reference to the Microsoft Scripting Runtime (you don't have to but it makes life so much easier) and try the following (converting brianb99999's code, from above):

    Dim oFSO As New Scripting.FileSystemObject
    Dim oTS As Scripting.File
    Dim strRetp As String

    '''' READ ROUTINE ''''''''''''''''''''''''''''''''''''''''''''''''''''
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForReading)

          Do Until oTS.AtEndOfStream

               strRetp = oTS.ReadLine
               MsgBox strRetp

          Loop

          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    '''' WRITE ROUTINE ''''''''''''''''''''''''''''''''''''''''''''''''''''
    strRept = "Hi there"
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForWriting, True)

          oTS.WriteLine "---------------------------"
          oTS.WriteLine strRetp
          oTS.WriteLine "---------------------------"
          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


    '''' APPEND ROUTINE '''''''''''''''''''''''''''''''''''''''''''''''''
    strRept = "Hi there"
    Set oTS = oFSO.OpenTextFile("x:\mypath\myfile.ext", ForAppending, True)

          oTS.WriteLine "---------------------------"
          oTS.WriteLine strRetp
          oTS.WriteLine "---------------------------"
          oTS.Close

    Set oTS = Nothing
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

This object-orientated approach also allows you to write Unicode (good for foreign characters).  You can use some other methods to read and write, with this approach, also:

    .Read(numberofcharacters)
    .ReadAll
    .Write "string to write"    '<-- NO NEW LINE
   
Look up the File System Object, for more info, here:

     http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObject.asp

HTH

J.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
brianb99999, I started with your code.  Now, I am working through.

wehoit, Thank you for the tips.

jimbobmcgee, Wow, very impressive.  As a beginer to txt file, this is kind of feedback I was hoping for.

Q1: If I use MyArray = Split(strLineJustRead, "|")
Do I need to have Dim MyArray(10) As Variant prior to this code for Split to work and produce:

MyArray(1)="A"
MyArray(1)=1234
MyArray(1)=#1/1/2005#
etc. ?

Q2: In case of a memo field, what is the max. string lenght I can handle with text file.  I wonder if wrap feature avaialbe here or I am limitted to say 255 caracters?

Regards,

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I figured the answer for Q1 from last post.

Thanks
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
0
dwieringaCommented:
In jimbobmcgee's post, I believe oTS should be defined as a Scripting.TextStream instead of Scripting.File
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.