12 Replies - 886 Views - Last Post: 09 October 2012 - 10:40 AM Rate Topic: -----

#1 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Problem reading from text file

Posted 08 October 2012 - 10:18 AM

Hey guys, can you help me understand why there is a line break in between each line?

'test.txt
'     John Doe, 21, 16, 1
'     Mary Doe, 20, 21, 2
'     Mike Doe, 20, 20, 1

Dim path As String = temp & "\test.txt"
Dim temp_array() As String = IO.File.ReadAllLines(path)

Dim temp_string as string = ""

For i as Integer = 0 to temp_array.Length - 1
temp_string &= i & " --- " & temp_array(i) & vbNewLine
Next       

textbox1.Text = temp_string

'output:
'     0 --- John Doe, 21, 16, 1
'     1 --- 
'     2 --- Mary Doe, 20, 21, 2
'     3 --- 
'     4 --- Mike Doe, 20, 20, 1
'     5 --- 



When I count the temp_array() I get 6 for the length.
Dim path As String = temp & "\test.txt"
Dim temp_array() As String = IO.File.ReadAllLines(path)
Dim count as Integer = temp_array.length 


This post has been edited by clee06: 08 October 2012 - 10:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem reading from text file

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Problem reading from text file

Posted 08 October 2012 - 10:24 AM

Try removing the vbNewline.

I strong suggest using a new list(of string) then adding through function .add(string) and the loop through all contents with no concatenation needed for a clean bug free display
Was This Post Helpful? 0
  • +
  • -

#3 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Problem reading from text file

Posted 08 October 2012 - 11:00 AM

View Posttrevster344, on 08 October 2012 - 10:24 AM, said:

Try removing the vbNewline.

I strong suggest using a new list(of string) then adding through function .add(string) and the loop through all contents with no concatenation needed for a clean bug free display



Ok I tried this, but my count is still 6. It should only be 3.

'test.txt
'     John Doe, 21, 16, 1
'     Mary Doe, 20, 21, 2
'     Mike Doe, 20, 20, 1

Dim path As String = "\test.txt"
Dim sreader As New System.IO.StreamReader(path)

        Do While sreader.Peek >= 0
            textList.Add(sreader.ReadLine)
        Loop

Dim count as Integer = textList.Count()
'count = 6



I guess I'll just do this:
...
Dim temp_string as String = ""
For Each line In textList
    If Not String.IsNullOrEmpty(line) Then
        temp_string &= line & vbNewLine
    End If
Next


This post has been edited by clee06: 08 October 2012 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Problem reading from text file

Posted 08 October 2012 - 11:11 AM

You sure that the text file is layout is not.
 John Doe, 21, 16, 1

 Mary Doe, 20, 21, 2

 Mike Doe, 20, 20, 1




Which your from your results is likely.

This post has been edited by AdamSpeight2008: 08 October 2012 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: Problem reading from text file

Posted 08 October 2012 - 11:17 AM

Thats quite strange that you are getting the length of array returned as 6. It should only return array of 3 items if the file has only 3 lines.

Were you using Notepad (default one available in windows) to check the data of the new file. If yes, try using a better editor like Notepad++. Notepad doesn't show the \r \n ones simply. It only shows \r\n for new lines afaik.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: Problem reading from text file

Posted 08 October 2012 - 11:26 AM

did you write the textfile somewhere? If so, can we see your code that does it?

The reason I ask is because you probably did something like:

MyWriter.WriteLine("Writing a line to the textfile" & Environment.Newline)


So, if you did, there's your extra newline character. See, writeline automagically handles the newline character so if you add it to the end of a string in a WriteLine you're adding a blank line.

This post has been edited by CharlieMay: 08 October 2012 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Problem reading from text file

Posted 08 October 2012 - 12:25 PM

I think i found my error. Its kinda complicated, I read a text file to a table, for some reason the first column has a newline with it. I think what I need to know is how to remove the newline in the first column. I recreate the csv file 5 times in excel, and check for all vbnewline and line break already.

This is the break down of what i was doing; read csv file to table, get records from table and save a temp_file on desktop, open up temp_file and read it line by line on a textbox. haha I know it makes no sense to you why I'm doing this.

I think SQL don't like this in my first column: (007-009). Because it has the "-", and all the other column are whole number, or excel change something in my file. idk

But this is my alternative solution.. I shouldn't avoid my problem, but its giving me a headache.

Dim temp_string As String = File.ReadAllText(path)
Dim temp_array() As String = temp_string.Split(vbNewLine)
Dim count as Integer = temp_array.length
'count = 2 
'success!


This post has been edited by clee06: 08 October 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: Problem reading from text file

Posted 08 October 2012 - 12:28 PM

So back to your temp file, can you show us the code where you create and save that temp file?



Also you mention reading a CSV file, have you looked into the TextFieldParser that makes reading CSV files effortless?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Problem reading from text file

Posted 08 October 2012 - 12:33 PM

There is even a tutorial about it, here.
Was This Post Helpful? 0
  • +
  • -

#10 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Problem reading from text file

Posted 08 October 2012 - 12:49 PM


...

Public Function getRecordTable(ByVal table_name As String)

        ...

        Dim col() As String = tableColumn(table_name)

        con = New MySqlConnection(dbpath)
        con.Open()
        query = "SELECT * FROM " & table_name
        cmd = New MySqlCommand(query, con)
        dr = cmd.ExecuteReader

        While dr.Read
            For i As Integer = 0 To col.Length - 1
                temp_string &= dr.Item(col(i)) & ","
            Next
            temp_string = temp_string.Substring(0, temp_string.Length - 1) 'remove the last coma
            new_string &= temp_string & vbNewLine
            temp_string = ""
        End While

        dbcon.Close()
        
        return new_string
End Function

...

If (Not System.IO.File.Exists(test_file)) Then
            Dim write_string As String = getRecordTable("testTable")
            Dim objWriter As New System.IO.StreamWriter(text_file)
            objWriter.Write(write_string)
            objWriter.Close()
End If




I have no clue what the TextFieldParser is, but I'll look into it if you say it helps.
Was This Post Helpful? 0
  • +
  • -

#11 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Problem reading from text file

Posted 09 October 2012 - 08:17 AM

I solve my problem. Here is the bits of code I use to check my string for any unwanted line carriage. Hope this help anyone who was puzzle or had the same problem..

new_string = old_string.Replace(vbCr, "").Replace(vbLf, "")


Was This Post Helpful? 0
  • +
  • -

#12 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Problem reading from text file

Posted 09 October 2012 - 08:40 AM

About a month ago, I submitted snippet, that clears line feed and carriage return from beginning and ending of text lines.
Was This Post Helpful? 0
  • +
  • -

#13 clee06  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 01-June 10

Re: Problem reading from text file

Posted 09 October 2012 - 10:40 AM

code snippet.. I haven't look this far in the forum yet haha, but thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1