none
Save Text File RRS feed

  • Question

  • Hi All,

    I have text file show bellow;

    3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
    5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
    0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274

    Then we want to remove last line by clicking Button_2.Then leave string in textbox2 as bellow;

    3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
    5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099

    Button_3 to saving back to the original file text.

    Option Strict On
    Option Infer Off
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
    
        Private Data As New List(Of String)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Text File=
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
            '0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274
    
            Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\D.txt"))
    
            For I As Integer = 0 To Data.Count - 1
                TextBox1.AppendText(Data(I) & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            TextBox2.Clear()
            For I As Integer = 0 To Data.Count - 2
                Me.TextBox2.AppendText(Data(I) & vbCrLf)
            Next
            'Textbox2 result show 
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
        End Sub
    
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Using fn As IO.StreamWriter = New IO.StreamWriter("C:\Users\family\Documents\D.txt", False)
    
                For I As Integer = 0 To TextBox2.Lines.Count - 1
                    fn.WriteLine(TextBox2.Lines(I))
                Next
                fn.Flush()
                fn.Close()
            End Using
    
    
    
        End Sub
    My problem is For I as Integer = 0 To Data.count - 2 not function


    • Edited by mipakteh Saturday, July 14, 2018 4:48 AM Add
    Saturday, July 14, 2018 4:46 AM

Answers

  • As Tommy shows, are there actually line breaks (CrLf characters) in the text file you load?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 1:18 PM
    Moderator
  • Yes .....yes.

    How to remove that ?.Load the text file has 2 line empty from the bottom.It Occur because deleted line or remove it.

    Either put a comma after every value and then read one line with commas. Next use split to make the list from the one comma delimited line.

    To make a file if using notepad then uncheck menu Format - Word Wrap to make one line.

    If saved in notepad with wordwrap on you will get the linefeeds on each line where it is wrapping.

    Visual studio has a text editor etc.

    3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127,5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099,0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274
    or have each value on a new line ie:

    3609
    2520
    6171
    8908
    5490
    7050
    0281
    9992
    1240
    6618
    7833
    0094
    6227
    5120
    2075
    9435
    3666
    9718
    9715
    8959
    3029
    6493
    6127
    5831
    5117
    2986
    8325
    0305
    7341
    9335
    7708
    6880
    8964
    8930
    6615
    3458
    3985
    8161
    4236
    8104
    0520
    8398
    6974
    5922
    9857
    1099
    0000
    5114
    6623
    4450
    9192
    2382
    6660
    5664
    0940
    2206
    4120
    5967
    6142
    7026
    6583
    2973
    7086
    3150
    5046
    4465
    7068
    0062
    6274


    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 2:31 PM

  • If (Data(Data.Count - 1) = "") Then
        Data.RemoveAt(Data.Count - 1)
    End If

    Or to remove *all* empty lines from the bottom:

    Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\D.txt"))
    
    While (Data(Data.Count - 1) = "")
        Data.RemoveAt(Data.Count - 1)
    End While
    

    - Wayne

    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 7:32 PM

All replies

  •     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            TextBox2.Clear()
            For I As Integer = 0 To Data.Count - 2
                Me.TextBox2.AppendText(Data(I) & vbCrLf)
            Next
            'Textbox2 result show 
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
        End Sub
    
    My problem is For I as Integer = 0 To Data.count - 2 not function


    "not function" *how*?

    What *exactly* seems to be the problem with it? You've been posting these
    threads here long enough by now that you should know you have to be specific.

    Does it not show something like this?

    - Wayne

    Saturday, July 14, 2018 10:15 AM
  • Thank for feeedback;

    Just test For ...Next statement.

       For I As Integer = 0 To data.Count - 1
                Me.TextBox1.AppendText(data(I) & vbCrLf)
            Next
            'Textbox1 result show 
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
            '0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274
    
            For I As Integer = 0 To data.Count - 2
                Me.TextBox2.AppendText(data(I) & vbCrLf)
            Next
            'Textbox2 result show 
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099
            '0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274
    
    
            For I As Integer = 0 To data.Count - 3
                Me.TextBox3.AppendText(data(I) & vbCrLf)
            Next
            'Textbox3 result show 
            '3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127
            '5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099

    Why For Count -2 not correct?

    Saturday, July 14, 2018 12:30 PM
  • Saturday, July 14, 2018 1:02 PM
  • As Tommy shows, are there actually line breaks (CrLf characters) in the text file you load?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 1:18 PM
    Moderator
  • Yes .....yes.

    How to remove that ?.Load the text file has 2 line empty from the bottom.It Occur because deleted line or remove it.

    Saturday, July 14, 2018 1:57 PM
  • That Reed kimble.That true.
    Saturday, July 14, 2018 1:58 PM
  • Yes .....yes.

    How to remove that ?.Load the text file has 2 line empty from the bottom.It Occur because deleted line or remove it.

    Either put a comma after every value and then read one line with commas. Next use split to make the list from the one comma delimited line.

    To make a file if using notepad then uncheck menu Format - Word Wrap to make one line.

    If saved in notepad with wordwrap on you will get the linefeeds on each line where it is wrapping.

    Visual studio has a text editor etc.

    3609,2520,6171,8908,5490,7050,0281,9992,1240,6618,7833,0094,6227,5120,2075,9435,3666,9718,9715,8959,3029,6493,6127,5831,5117,2986,8325,0305,7341,9335,7708,6880,8964,8930,6615,3458,3985,8161,4236,8104,0520,8398,6974,5922,9857,1099,0000,5114,6623,4450,9192,2382,6660,5664,0940,2206,4120,5967,6142,7026,6583,2973,7086,3150,5046,4465,7068,0062,6274
    or have each value on a new line ie:

    3609
    2520
    6171
    8908
    5490
    7050
    0281
    9992
    1240
    6618
    7833
    0094
    6227
    5120
    2075
    9435
    3666
    9718
    9715
    8959
    3029
    6493
    6127
    5831
    5117
    2986
    8325
    0305
    7341
    9335
    7708
    6880
    8964
    8930
    6615
    3458
    3985
    8161
    4236
    8104
    0520
    8398
    6974
    5922
    9857
    1099
    0000
    5114
    6623
    4450
    9192
    2382
    6660
    5664
    0940
    2206
    4120
    5967
    6142
    7026
    6583
    2973
    7086
    3150
    5046
    4465
    7068
    0062
    6274


    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 2:31 PM
  • From load text file, How to remove the empty line at the bottom.
    Saturday, July 14, 2018 2:54 PM
  • From load text file, How to remove the empty line at the bottom.

    In Notepad turn word wrap off. With caret at left of line 2 press backspace key to delete chars at left of line 2 until it is moved by the editor to the end of line 1.

    That is a data editing question you should search that web for how that is done or any other data editing questions you have.

    :)

    Saturday, July 14, 2018 3:39 PM
  • From load text file, How to remove the empty line at the bottom.

    Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\D.txt"))
    
    If (Data(Data.Count - 1) = "") Then
        Data.RemoveAt(Data.Count - 1)
    End If
    

    - Wayne

    Saturday, July 14, 2018 7:20 PM

  • If (Data(Data.Count - 1) = "") Then
        Data.RemoveAt(Data.Count - 1)
    End If

    Or to remove *all* empty lines from the bottom:

    Data.AddRange(IO.File.ReadAllLines("C:\Users\family\Documents\D.txt"))
    
    While (Data(Data.Count - 1) = "")
        Data.RemoveAt(Data.Count - 1)
    End While
    

    - Wayne

    • Marked as answer by mipakteh Sunday, July 15, 2018 2:06 AM
    Saturday, July 14, 2018 7:32 PM
  • Thank WayNe King.....
    Sunday, July 15, 2018 2:05 AM
  • Thank Tommy...
    Sunday, July 15, 2018 2:06 AM
  • Thank Reed
    Sunday, July 15, 2018 2:06 AM