-
Feb 9th, 2016, 01:34 AM
#1
Thread Starter
New Member
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
-
Feb 9th, 2016, 01:39 AM
#2
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.
-
Feb 9th, 2016, 01:54 AM
#3
Thread Starter
New Member
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
-
Feb 9th, 2016, 03:26 AM
#4
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:
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; } }
-
Feb 9th, 2016, 03:31 AM
#5
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:
using (var outputfile = File.CreateText("Distance.txt")) { while(...) { // ... } }
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.
-
Feb 9th, 2016, 03:33 AM
#6
Re: Creating text files
Originally Posted by jmcilhinney
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.
-
Feb 9th, 2016, 05:04 AM
#7
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
-
Feb 9th, 2016, 05:30 AM
#8
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
-
Feb 9th, 2016, 06:17 AM
#9
Re: Creating text files
Originally Posted by FunkyDexter
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.
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.
-
Feb 9th, 2016, 08:42 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|