Reading and Writing Text Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bbepristis

    #1

    Reading and Writing Text Files

    Hey all I have this code that reads from one text file writes to
    another unless im on a certian line then it writes the new data however
    it only seems to do about 40 lines then quits and I cant figure out why
    any help would be much apprechiated

    1.
    Dim line_num As Integer
    2.
    Dim filename2 As String
    3.
    Dim csv_text As String
    4.
    csv_text = companytxt.Text & "," & Nametxt.Text & "," &
    addrtxt.Text & "," & citytxt.Text & "," & statetxt.Text & "," &
    ziptxt.Text & "," & countrytxt.Text & "," & phonetxt.Text & "," &
    faxtxt.Text & "," & celltxt.Text & "," & titletxt.Text & "," &
    assistanttxt.Te xt & "," & urltxt.Text & "," & altcontacttxt.T ext & ","
    & altphonetxt.Tex t & "," & altaddrtxt.Text & "," & altcitytxt.Text &
    "," & altstatetxt.Tex t & "," & altziptxt.Text & "," & territorytxt.Te xt
    & "," & mech1serialnum. Text & "," & mech1vacpump.Te xt & "," &
    mech1spindle.Te xt & "," & mech1vacpumpser ialnum.Text & "," &
    mech1filterpart num.Text & "," & mech2serialnum. Text & "," &
    mech2vacpump.Te xt & "," & mech2spindle.Te xt & "," &
    mech2vacpumpser ialnum.Text & "," & mech2filterpart num.Text & "," &
    mech3serialnum. Text & "," & mech3vacpump.Te xt & "," & mech3spindle.Te xt
    & "," & mech3vacpumpser ialnum.Text & "," & mech3filterpart num.Text &
    "," & mech4serialnum. Text & "," & mech4vacpump.Te xt & "," &
    mech4spindle.Te xt & "," & mech4vacpumpser ialnum.Text & "," &
    mech4filterpart num.Text & "," & user1txt.Text & "," & user2txt.Text &
    "," & user3txt.Text & "," & user4txt.Text & "," & user5txt.Text & "," &
    user6txt.Text & "," & user7txt.Text & "," & user8txt.Text & "," &
    user9txt.Text & "," & user10txt.Text & "," & user11txt.Text & "," &
    user12txt.Text & "," & user13txt.Text & "," & user14txt.Text & "," &
    user15txt.Text
    5.
    filename2 = "C:\Documen ts and Settings\Brian Bepristis\My
    Documents\Visua l Studio Projects\Multic amtemp.csv"
    6.
    Dim filename As String
    7.
    filename = "C:\Documen ts and Settings\Brian Bepristis\My
    Documents\Visua l Studio Projects\Multic amtemp2.csv"
    8.
    ' Create an empty string array to return to caller.
    9.
    Dim lines() As String = {}
    10.

    11.
    ' Check to see if the file exists.
    12.
    If File.Exists(fil ename) Then
    13.
    ' Open a stream reader to get the text from the file.
    14.
    Dim sr As New StreamReader(fi lename)
    15.
    ' Read all the file text.
    16.
    Dim fileText As String = sr.ReadToEnd()
    17.
    ' Close the stream reader
    18.
    sr.Close()
    19.

    20.
    ' Split the text into a string array delimited by
    Carriage Return/Line Feed.
    21.
    lines = Split(fileText, vbCrLf)
    22.

    23.
    ' Return
    24.
    Dim fs As New FileStream(file name2, FileMode.Create )
    25.
    Dim sw As New StreamWriter(fs )
    26.

    27.
    Dim foundBlank As Boolean
    28.
    For Each line As String In lines
    29.
    If line.Length > 0 Then
    30.
    line_num += 1
    31.
    If line_num = currecord Then
    32.
    sw.WriteLine(cs v_text)
    33.
    ' Reset blank line flag
    34.
    foundBlank = False
    35.
    Else
    36.
    sw.WriteLine(li ne)
    37.
    ' Reset blank line flag
    38.
    foundBlank = False
    39.
    End If
    40.
    Else
    41.
    If Not foundBlank Then
    42.
    ' Blank line: write first one
    43.
    sw.WriteLine(li ne)
    44.
    ' Set flag to indicate that blank line
    was found
    45.
    foundBlank = True
    46.
    End If
    47.
    End If
    48.
    Next
    49.
    sw.Close()
    50.
    fs.Close()
    51.
    End If
    52.
    If System.IO.File. Exists(filename 2) = True Then
    53.
    If System.IO.File. Exists(filename ) = True Then
    54.
    System.IO.File. Delete(filename )
    55.
    End If
    56.
    System.IO.File. Copy(filename2, filename)
    57.
    End If

  • Cor Ligthert [MVP]

    #2
    Re: Reading and Writing Text Files

    Are you sure you don't have included a chrw(0) value.

    After that you cannot see anything anymore.

    I hope this helps,

    Cor

    "bbepristis " <brian.bepristi s@gmail.com> schreef in bericht
    news:1147377282 .880777.127320@ v46g2000cwv.goo glegroups.com.. .[color=blue]
    > Hey all I have this code that reads from one text file writes to
    > another unless im on a certian line then it writes the new data however
    > it only seems to do about 40 lines then quits and I cant figure out why
    > any help would be much apprechiated
    >
    > 1.
    > Dim line_num As Integer
    > 2.
    > Dim filename2 As String
    > 3.
    > Dim csv_text As String
    > 4.
    > csv_text = companytxt.Text & "," & Nametxt.Text & "," &
    > addrtxt.Text & "," & citytxt.Text & "," & statetxt.Text & "," &
    > ziptxt.Text & "," & countrytxt.Text & "," & phonetxt.Text & "," &
    > faxtxt.Text & "," & celltxt.Text & "," & titletxt.Text & "," &
    > assistanttxt.Te xt & "," & urltxt.Text & "," & altcontacttxt.T ext & ","
    > & altphonetxt.Tex t & "," & altaddrtxt.Text & "," & altcitytxt.Text &
    > "," & altstatetxt.Tex t & "," & altziptxt.Text & "," & territorytxt.Te xt
    > & "," & mech1serialnum. Text & "," & mech1vacpump.Te xt & "," &
    > mech1spindle.Te xt & "," & mech1vacpumpser ialnum.Text & "," &
    > mech1filterpart num.Text & "," & mech2serialnum. Text & "," &
    > mech2vacpump.Te xt & "," & mech2spindle.Te xt & "," &
    > mech2vacpumpser ialnum.Text & "," & mech2filterpart num.Text & "," &
    > mech3serialnum. Text & "," & mech3vacpump.Te xt & "," & mech3spindle.Te xt
    > & "," & mech3vacpumpser ialnum.Text & "," & mech3filterpart num.Text &
    > "," & mech4serialnum. Text & "," & mech4vacpump.Te xt & "," &
    > mech4spindle.Te xt & "," & mech4vacpumpser ialnum.Text & "," &
    > mech4filterpart num.Text & "," & user1txt.Text & "," & user2txt.Text &
    > "," & user3txt.Text & "," & user4txt.Text & "," & user5txt.Text & "," &
    > user6txt.Text & "," & user7txt.Text & "," & user8txt.Text & "," &
    > user9txt.Text & "," & user10txt.Text & "," & user11txt.Text & "," &
    > user12txt.Text & "," & user13txt.Text & "," & user14txt.Text & "," &
    > user15txt.Text
    > 5.
    > filename2 = "C:\Documen ts and Settings\Brian Bepristis\My
    > Documents\Visua l Studio Projects\Multic amtemp.csv"
    > 6.
    > Dim filename As String
    > 7.
    > filename = "C:\Documen ts and Settings\Brian Bepristis\My
    > Documents\Visua l Studio Projects\Multic amtemp2.csv"
    > 8.
    > ' Create an empty string array to return to caller.
    > 9.
    > Dim lines() As String = {}
    > 10.
    >
    > 11.
    > ' Check to see if the file exists.
    > 12.
    > If File.Exists(fil ename) Then
    > 13.
    > ' Open a stream reader to get the text from the file.
    > 14.
    > Dim sr As New StreamReader(fi lename)
    > 15.
    > ' Read all the file text.
    > 16.
    > Dim fileText As String = sr.ReadToEnd()
    > 17.
    > ' Close the stream reader
    > 18.
    > sr.Close()
    > 19.
    >
    > 20.
    > ' Split the text into a string array delimited by
    > Carriage Return/Line Feed.
    > 21.
    > lines = Split(fileText, vbCrLf)
    > 22.
    >
    > 23.
    > ' Return
    > 24.
    > Dim fs As New FileStream(file name2, FileMode.Create )
    > 25.
    > Dim sw As New StreamWriter(fs )
    > 26.
    >
    > 27.
    > Dim foundBlank As Boolean
    > 28.
    > For Each line As String In lines
    > 29.
    > If line.Length > 0 Then
    > 30.
    > line_num += 1
    > 31.
    > If line_num = currecord Then
    > 32.
    > sw.WriteLine(cs v_text)
    > 33.
    > ' Reset blank line flag
    > 34.
    > foundBlank = False
    > 35.
    > Else
    > 36.
    > sw.WriteLine(li ne)
    > 37.
    > ' Reset blank line flag
    > 38.
    > foundBlank = False
    > 39.
    > End If
    > 40.
    > Else
    > 41.
    > If Not foundBlank Then
    > 42.
    > ' Blank line: write first one
    > 43.
    > sw.WriteLine(li ne)
    > 44.
    > ' Set flag to indicate that blank line
    > was found
    > 45.
    > foundBlank = True
    > 46.
    > End If
    > 47.
    > End If
    > 48.
    > Next
    > 49.
    > sw.Close()
    > 50.
    > fs.Close()
    > 51.
    > End If
    > 52.
    > If System.IO.File. Exists(filename 2) = True Then
    > 53.
    > If System.IO.File. Exists(filename ) = True Then
    > 54.
    > System.IO.File. Delete(filename )
    > 55.
    > End If
    > 56.
    > System.IO.File. Copy(filename2, filename)
    > 57.
    > End If
    >[/color]


    Comment

    • bbepristis

      #3
      Re: Reading and Writing Text Files

      not sure im new to vb so Im not sure what you mean

      Comment

      • bbepristis

        #4
        Re: Reading and Writing Text Files

        Nevermind figured it out there was some funky charicters in the csv
        file stoping it..

        Chars were --- NSB@ VL? ---

        Comment

        Working...