Results 1 to 10 of 10

Thread: Creating text files

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Creating text files

    decimal speed, distance;
    int hours;
    int count = 1;
    StreamWriter outputfile;
    if (decimal.TryParse(textBox1.Text, out speed))
    {
    if (int.TryParse(textBox2.Text, out hours))
    {
    while (count <= hours )
    {


    distance = speed * count;
    outputfile = File.CreateText("Distance.txt");
    outputfile.WriteLine("After hour " + count + " the distance is " + distance.ToString());
    outputfile.Close();
    MessageBox.Show("The distance was counted.");
    count = count + 1;
    }
    }

    In the text files it shows only the one line . for example , if i enter 40(speed) and 3(hour) : After hour 3 the distance is 120. But i want it to show each hour. like hour 1 hour 2 hour 3. soo whats wrong here

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating text files

    This is the VB.NET forum. Not surprisingly, it is for VB.NET questions. If you're posting C# code then you have a C# question, so it should be posted in the C# forum. I have asked the mods to move this thread.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    4

    Creating text files

    decimal speed, distance;
    int hours;
    int count = 1;
    StreamWriter outputfile;
    if (decimal.TryParse(textBox1.Text, out speed))
    {
    if (int.TryParse(textBox2.Text, out hours))
    {
    while (count <= hours )
    {


    distance = speed * count;
    outputfile = File.CreateText("Distance.txt");
    outputfile.WriteLine("After hour " + count + " the distance is " + distance.ToString());
    outputfile.Close();
    MessageBox.Show("The distance was counted.");
    count = count + 1;
    }
    }

    In the text files it shows only the one line . for example , if i enter 40(speed) and 3(hour) : After hour 3 the distance is 120. But i want it to show each hour. like hour 1 hour 2 hour 3. soo whats wrong here

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating text files

    I specifically told you in your other thread that I had asked the mods to move it. Now you have a duplicate thread. I have now asked the mods to delete that other thread.

    Also, for readability, please use formatting tags when posting code snippets, i.e.

    [code]your code here[/code]

    or

    [highlight=csharp]your code here[/highlight]

    As you can see, it makes things much clearer:
    csharp Code:
    1. decimal speed, distance;
    2.             int hours;
    3.             int count = 1;
    4.             StreamWriter outputfile;
    5.             if (decimal.TryParse(textBox1.Text, out speed))
    6.             {
    7.                 if (int.TryParse(textBox2.Text, out hours))
    8.                 {
    9.                     while (count <= hours  )
    10.                     {
    11.                        
    12.                        
    13.                         distance = speed * count;
    14.                         outputfile = File.CreateText("Distance.txt");
    15.                         outputfile.WriteLine("After hour " + count + " the distance is " + distance.ToString());
    16.                         outputfile.Close();
    17.                         MessageBox.Show("The distance was counted.");
    18.                        count = count + 1;  
    19.                     }
    20.                 }

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating text files

    As for the issue, you are creating the file inside the loop, therefore you write only one line to each file and overwrite it on the next iteration. You need to create the file before the loop and then close it afterwards. That would be done most correctly with a `using` statement:
    csharp Code:
    1. using (var outputfile = File.CreateText("Distance.txt"))
    2. {
    3.     while(...)
    4.     {
    5.         // ...
    6.     }
    7. }
    The object you create with the `using` statement is disposed at the end of the block. Disposing a `StreamReader` includes closing the file. Many things that get logically "opened" can be closed this way, guaranteeing that you won't leak resources even if an exception is thrown.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating text files

    Quote Originally Posted by jmcilhinney View Post
    I specifically told you in your other thread that I had asked the mods to move it. Now you have a duplicate thread. I have now asked the mods to delete that other thread.
    Not only that, I just realised that you have still posted in the wrong forum. This is the C# CodeBank forum, not the C# forum. CodeBank forums are where you can share working code with others. They are not for asking questions. I have now asked the mods to move THIS thread to the correct forum.

  7. #7
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Creating text files

    Moved to C# forum.

    I'm not sure it's your problem but there's no need to Call File.CreateText repeatedly in the loop. Just call that once before you start looping.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Creating text files

    Threads merged. Deleted a couple of posts for clarity. @neilyd, I'd urge you to spend 5 minutes or so familiarising yourself with the various sub forums we have available. You'll get much better answers to your questions if you post them in the right area.

    JM, I'm not sure File.CreateText being inside the loop is actually the problem. That was my first assumption but I think it appends rather than overwrites if the file already exists. I didn't spend much time checking it though so I could be wrong.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Creating text files

    Quote Originally Posted by FunkyDexter View Post
    JM, I'm not sure File.CreateText being inside the loop is actually the problem. That was my first assumption but I think it appends rather than overwrites if the file already exists. I didn't spend much time checking it though so I could be wrong.
    I thought I'd check to confirm because I didn't previously either.
    Quote Originally Posted by MSDN
    This method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten.

  10. #10
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Creating text files

    I stand corrected
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

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