ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Line Input is reading extra lines? (https://www.excelbanter.com/excel-programming/450711-line-input-reading-extra-lines.html)

Robert Crandal[_3_]

Line Input is reading extra lines?
 
So, I've been playing with the "Line Input" command to test
reading from text files. I am reading and displaying each
paragraph with a MsgBox. If there are only 2 paragraphs
in my input, why is a MsgBox appearing 4 times?

It actually prints 2 empty Msgbox windows, so does that
mean my input has extra CR or LF characters? Do I
need to filter out blank lines? (P.S: I know Gary showed
me a filter function, but that worked with a different
type of variable.)

Here is my sample text data:

'--------------------begin "inp.txt"------------------------
Depart do be so he enough talent. Sociable formerly six but handsome. Up do
view time they shot. He concluded disposing provision by questions as
situation. Its estimating are motionless day sentiments end. Calling an
imagine at forbade. At name no an what like spot. Pressed my by do affixed
he studied.

The him father parish looked has sooner. Attachment frequently gay
terminated son. You greater nay use prudent placing. Passage to so distant
behaved natural between do talking. Friends off her windows painful. Still
gay event you being think nay for. In three if aware he point it. Effects
warrant me by no on feeling settled resolve.
'--------------------end "inp.txt"------------------------

Here is my code:

Sub myImportTxt()

Dim sFile As String
Dim sLine As String
Dim f As Integer

f = FreeFile()
sFile = "inp.txt"

Open sFile For Input As #f

Do Until EOF(f)
Line Input #f, sLine
MsgBox sLine ' MsgBox shows 4 times?
Loop ' Why? How fix?

Close #f

End Sub





GS[_2_]

Line Input is reading extra lines?
 
The structure of the text file will dictate what results you get! For
example, if you copy 2 paragraphs from a Word doc and paste them into a
text editor (or a worksheet), you end up with 3 lines (or rows in wks),
regardless of the number of sentences in each paragraph. A CrLf denotes
a new line. Usually, paragraphs are separated by a blank line, ergo the
3 lines in the copy/paste example. So in this case the paragraph
delimiter is a blank empty line. This complicates parsing the Word doc.

When text files are properly structured to store data, the 1st line
should contain fieldnames, the remaining lines are individual records,
and there should be no blank lines anywhere in the file.

If the Word doc above was structured to use paragraph spacing instead
of a blank CrLf, the doc would parse more eaily because the paragraph
separator will be a CrLf, same as the record separator in a properly
structured data file.

Go back to the Word doc an delete the empty line between the
paragraphs, and set spacing before to render the desired 'appearance'
of how the 2nd paragraph is spaced from the 1st. This structor would be
dictated by Styles defs in a document template so the content conforms
to some consistent standard. While positioning content via the space,
tab, and enter keys for simple printing is the general practice, people
get away with it because printing is a WYSIWYG process. Not the case
when converting that doc to other file formats suitable for ereader
devices because the rendered results are highly unpredictable. The
template structure I described earlier renders predictable results in
all output formats I've generated (11 to date). Note that output to a
PDF file is not so much a conversion as it is a WYSIWYG printout.

Probably more info here than needed, but importing text to a worksheet
can be a very confusing process to one who doesn't understand file
structure concepts!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



joeu2004[_2_]

Line Input is reading extra lines?
 
"Robert Crandal" wrote:
Sub myImportTxt()
Dim sFile As String
Dim sLine As String
Dim f As Integer

f = FreeFile()
sFile = "inp.txt"
Open sFile For Input As #f
Do Until EOF(f)
Line Input #f, sLine
MsgBox sLine ' MsgBox shows 4 times?
Loop ' Why? How fix?
Close #f
End Sub


The only time I see an "extra" iteration is when there is, indeed, an extra
line at the end.

You can guard against that with the following change to your loop:

Line Input #f, sLine
If Len(sLine) 0 Or Not EOF(f) Then MsgBox sLine
End If

You can guard against many empty lines at the end as follows:

Dim b As Long
b = 0
Do Until EOF(f)
Line Input #f, sLine
If Len(sLine) = 0 Then
b = b + 1
Else
For b = b To 1 Step -1
MsgBox
Next
MsgBox sLine
End If
Loop


Maurizio Borrelli[_2_]

Line Input is reading extra lines?
 
Il giorno sabato 14 marzo 2015 01:24:21 UTC+1, Robert Crandal ha scritto:
So, I've been playing with the "Line Input" command to test
reading from text files. I am reading and displaying each
paragraph with a MsgBox. If there are only 2 paragraphs
in my input, why is a MsgBox appearing 4 times?

It actually prints 2 empty Msgbox windows, so does that
mean my input has extra CR or LF characters? Do I
need to filter out blank lines? (P.S: I know Gary showed
me a filter function, but that worked with a different
type of variable.)

Here is my sample text data:

'--------------------begin "inp.txt"------------------------
Depart do be so he enough talent. Sociable formerly six but handsome. Up do
view time they shot. He concluded disposing provision by questions as
situation. Its estimating are motionless day sentiments end. Calling an
imagine at forbade. At name no an what like spot. Pressed my by do affixed
he studied.

The him father parish looked has sooner. Attachment frequently gay
terminated son. You greater nay use prudent placing. Passage to so distant
behaved natural between do talking. Friends off her windows painful. Still
gay event you being think nay for. In three if aware he point it. Effects
warrant me by no on feeling settled resolve.
'--------------------end "inp.txt"------------------------

Here is my code:

Sub myImportTxt()

Dim sFile As String
Dim sLine As String
Dim f As Integer

f = FreeFile()
sFile = "inp.txt"

Open sFile For Input As #f

Do Until EOF(f)
Line Input #f, sLine
MsgBox sLine ' MsgBox shows 4 times?
Loop ' Why? How fix?

Close #f

End Sub


Hi Robert,

try:

Public Sub Test()
Const cstrPath = "D:\"
Const cstrFile = "Line Input is reading extra lines.txt"
Dim ff As Integer
Dim s() As String
Dim i As Long

ff = FreeFile(0)
Open cstrPath & cstrFile For Input Access Read As ff
s = Split(Input$(LOF(ff), #ff), vbNewLine)
Close

Debug.Print "Items : "; UBound(s) + 1

For i = LBound(s) To UBound(s)
If Len(s(i)) Then Debug.Print s(i)
Next

End Sub

--
Ciao!
Maurizio


All times are GMT +1. The time now is 04:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com