Community discussion forum

insert two words into a TextFile in a sigle line ;these two words arranged in left alignment each other

Tags: .net India
  • 1 year ago

    Hi Friends,

                enter two words line by line into a text file

    example:   if i pass two words in a line    Hai , Wish

    next line Hello, Wish

    next line True,Equal

    .How these words arranged these words like this

    Hai          Wish

    Hello       Wish

    True        Equal 

    ie

     

     

  • 1 year ago

    You can't insert anything into an existing text file.  You would have to read the contents of the file in and then write it out again with the new text inserted at the appropriate position.

    You might choose to read the entire file in, then insert the text, then write the whole file out.  Alternatively you might choose to read and write a line at a time, writing the extra text at the appropriate point.  If you choose the second option you will not be able to write to the same path.  You'd have to write to a temp location and then overwrite the original file when you were done.

  • 1 year ago

    Sounds like homework to me:

    Enter two words into a text box, seperated by a comma.

    Write these to a text file, seperated by a tab character, or at a fixed spacing.

     

    If so, look at the StreamWriter class for examples of how to write to a text file.

     eg  if item1 and item2 are strings, taken from a text box on your form:

          ' Create an instance of StreamWriter to write text to a file.
            Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
            ' Add some text to the file.
            sw.WriteLine(Item1 + "  " + item2)
            sw.Close()

     

  • 1 year ago

    Mr Gefftullin,

                      Thanks for your replay. My problem is the space between two words.i want to align  the second word in all line are left aligned

                  ie:-     hdgfhgfhdgf      djfhgf

                          sdfsdfs             fgsfgfd

                          df                     fdffdff

    i wrote these codes  but the second words are not in a  stright line

     it shoudn't  work  properly

    this is the code :

    Dim file As New System.IO.StreamWriter("c:\test.txt", True)

    Dim ss As String

    Dim i, l As Integer

    l = txtwords.Text.Trim.Length

    For i = l To 10

    ss += vbTab

    Next

    MsgBox(ss.Length + txtwords.Text.Trim.Length)

    file.WriteLine(txtwords.Text.Trim & ss & txtotherwords.Text.Trim)

    file.Close()

      if you get any other idea ?

     

     

     

     

     

  • 1 year ago
    Use the PadRight method to add spaces to the first word in each line to make each the same width.  That way the second word in each line will each start at the same column.  The contents of the file will have to be viewed in a fixed-width font for that to be useful though.  Notepad uses a fixed-width font by default.  A .NET TextBox does not.
  • 1 year ago

     

                                My  problem is solved, I insert a tab character in between the two words like this

     

    Dim File1 As New System.IO.StreamWriter("C:\test.txt",True)
    File1.WriteLine(Textbox1.Text  & chr(9) & TextBox2.Text)
    File1.Close

    Now all second word are leftaligned at any string length

    if  insert space ("  ") instead of tabcharcter we cannot get such result.

                                     Thanks  Everybody to help me to get right result

                                                      Pratheej

     

     

     

  • 1 year ago

    Your code does work because Textbox1.Text.Length does not exceed a certain value. Please try "abc" as first word and then "0123456789abc" as first word in the next line.

    To overcome this problem, you can insert a number of TABs or CHR(9)s depending on the length of Textbox1.Text.

  • 1 year ago

    [quote user="Zorro_ot"]

    Your code does work because Textbox1.Text.Length does not exceed a certain value. Please try "abc" as first word and then "0123456789abc" as first word in the next line.

    To overcome this problem, you can insert a number of TABs or CHR(9)s depending on the length of Textbox1.Text.

    [/quote] Or you could just use PadRight as I suggested.

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback