Community discussion forum

Write new line to text file in C#

Tags: csharp China
  • 2 years ago

    Good day to all,

    I am very much new to programming and C#. I have a problem in writing a new line to a text file. I am using a streamwriter to create a new text file and write data to it. everytime, I add a newline to textfile, it should be like below:

             John;1234;somedata;somedata
             Lily;2213;somedata;somedata

    however, my code wont do the above, the output in the text is John;1234;somedata;somedataLily;2213;somedata;somedata   which is not what i want. Please show me the correct way to fix this problem. I will be very appreciated.

     

  • 2 years ago
    hi there,

    sWriter.Write(" John;1234;somedata;somedata\r\nLily;2213;somedata;somedata");


  • 2 years ago

    I think what you are looking for is writeline, instead of write.

    That automatically puts an end-of-line sequence at the end of each line you output.

    sWriter.WriteLine(" John;1234;somedata;somedata")

  • 2 years ago

    Thanks for your reply jefftullin,

    but i tried that code in my app and it didn't work. my code is something like below:

    streamwriter sw = new streamwriter("file.txt:, true, Encoding.UTF8);

    sw.writeline( "input from text box");

     I take the input from several textbox, and write it to text box in one line seperated by a ";"

    does it have to do with buffer, or do i have to creat a loop somehow?

  • 2 years ago

    Your original post suggested that you wanted the output to look like:

    John;1234;somedata;somedata
    Lily;2213;somedata;somedata

    To do that, use

    WriteLine("John;1234;somedata;somedata")
    WriteLine("Lily;2213;somedata;somedata")

    If what you actually wanted is

    John

    1234

    somedata

    somedata

    Then Writeline will still help, but you need to do a bit more work.

    First, why add up all the textboxes if you want to write them to the file piece by piece?

    eg what wrong with

    WriteLine(text1.text)

    WriteLine(text2.text)  etc?

     

    If you must start with "John;1234;somedata;somedata"

    then perhaps you could look into using the Replace()  function to swap all the semicolons for \r\n  end of line sequences.



     

  • 2 years ago

    Another possibility is to use the Environment.NewLine statement.

    Here is an example:

    Console.WriteLine(string.Format("Line1{0}Line2{0}", Environment.NewLine);

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

  • 2 years ago

    thanks you for taking your time reply to my post. I got this working last nite already. What my program does is, take input from several text box and write it to a single line. every time a button add is clicked, a new line will be added. i did use the writeline method and this time it's worked. My only problem now is how to search for data in a line by using one of its text field.

    for example: here is the data in text file

    John;1234;Sydney;Aus
    Timy;3214;Melbourne;Aus
    Lana;8471;Darwin;Aus

    now i want to bring up all of timy's data by searching for "timy". I know I have to use some kind of loop for this function, but i not so sure how to? any suggestion will be a great help for me. Tah!

  • 2 years ago

    Hi,

     I don't know the entire context but if you want to save objects and reload them afterwards, it might be easier to use Binary Serialization.

    Here is an example: http://www.codeguru.com/columns/DotNet/article.php/c6595/.

    If that doesn't help you, you can aswell use the StreamReader. The class contains a method named ReadLine(). Within the returned string from ReadLine() you then use string functions to check for a given name. This means that you need to read until you find the correct line.

     Greetz,

     Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

  • 2 years ago

    Hi Geert,

    That's a very useful link. I will look into it.

    For now i will use streamreader to obtain the data since i am very new to C#. just a question, how do I read till find the correct line, does it mean that i have to create a loop somehow?

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