Reading a text file: Commas behave as separators

O

Ogier

I have a text file containing the following four lines:

This line has no commas.
However, this one has
But, alas, the line is read in pieces
Characters: ;_.+"%&!,,,

If I try to read it with the following sub

Private Sub ReadTextFile()
Dim FileName As String
Dim Line As String
FileName = "Y:\TestTextWithCommas.txt"
Open FileName For Input As #1
Do Until EOF(1)
Input #1, Line
Debug.Print Line
Loop
Close #1
End Sub

The result in the Immediate window is this:

This line has no commas.
However
this one has
But
alas
the line is read in pieces
Characters: ;_.+"%&!
[Three empty lines follow]

What is the reason for this strange behavior and how do I avoid it?

Best wishes
Holger Nielsen
 
J

Jay Freedman

Change your code from

Input #1, Line

to

Line Input #1, Line

From the help topic, "The Line Input # statement reads from a file one
character at a time until it encounters a carriage return (Chr(13)) or
carriage return-linefeed (Chr(13) + Chr(10)) sequence."

In contrast, the Input # statement is meant to read comma-delimited data
created by the Write # statement. It isn't appropriate for the sort of data
you're trying to read.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top