CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    5

    [RESOLVED] How to add more spaces between lines to a text file ?

    Hi,

    tw.WriteLine(tw.NewLine);

    I have this line wich add a space between the text in the text file i created .
    Each time i press the button a new line is added with spaces between it .

    How do i set how much spaces to add between the lines ?
    For example now its making two empty line between each text how do i set it so it will make one space or 6 spaces how do i do it ?

    This is the full code :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace FileWriteRead
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }




    private void button1_Click(object sender, EventArgs e)
    {

    TextWriter tw = new StreamWriter(@"d:\date.txt",true);
    tw.WriteLine(DateTime.Now);
    tw.WriteLine(tw.NewLine);
    tw.Close();
    }

    }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to add more spaces between lines to a text file ?

    You use a loop when adding the new lines.

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: How to add more spaces between lines to a text file ?

    Quote Originally Posted by Chocolade View Post
    Hi,

    tw.WriteLine(tw.NewLine);

    I have this line wich add a space between the text in the text file i created .
    Each time i press the button a new line is added with spaces between it .

    How do i set how much spaces to add between the lines ?
    For example now its making two empty line between each text how do i set it so it will make one space or 6 spaces how do i do it ?
    I do wish you wouldnt refer to a new line as a "space". A Space is what you get when you press the Space Bar -> " ", not the Return/Enter key


    As Ed says, call WriteLine() [with no arguments!] in a loop:

    Code:
    //write 10 newlines
    for int i=0;i<10;i++)
      tw.WriteLine();
    
    //or you can perform this hack:
    tw.WriteLine( string.Join(Environment.NewLine, new string[10]) );
    Yep, joining an array of 10 items using newline will result in a sequence of 9 new lines (one between each of the 10 empty elements). We call WriteLine to add the 10th.. thus 10 new lines are written to the file
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured