For Loop with Random Never Stops!
I am using the following code to create a dummy file of a specific size.
RemSpace declared as a Long value and is returned in bytes from a separate function in my applciation. I have verified that it returns the correct # in bytes. However, when I execute the following routine, the dummyfile is created, but it never stops the loop. I have to kill it from task manager or it would fill up the HDD. What am I missing here? The line:
Code:
For i As Integer = 1 To RemSpace
should tell the For loop to stop when it reaches the value of RemSpace, which is a long value in bytes. Here is my complete code.
Thanks for any help with this one.
Code:
Public Sub Create_DummyFile()
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next()
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
For i As Integer = 1 To RemSpace
myStreamWriter.Write(RandomNumber)
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
End Sub
Re: For Loop with Random Never Stops!
The line For i As Integer = 1 To RemSpace is requesting that i which is an integer, is incremented to RemSpace which is a Long. The maximum value for an integer is -2,147,483,648 to +2,147,483,647.
Is RemSpace bigger than that?
Re: For Loop with Random Never Stops!
hmm.....
RemSpace = 123971584
Declaring i as a long value produces the same results. I guess I am misunderstanding here. I guess RemSpace should be declared as an integer as well?
Re: For Loop with Random Never Stops!
ok, here's what Id do, put a Label on your form (I'll call it Label1) and add this code in bold;
Code:
Public Sub Create_DummyFile()
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next()
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
For i As Integer = 1 To RemSpace
myStreamWriter.Write(RandomNumber)
Label1.Text = i.ToString & " (123971584)"
Label1.Refresh()
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
End Sub
Now run the program and watch the value in the label, does it exceed RemSpace?
Writing 123971584 numbers to a file, might take a while, so is it the case that its just taking longer than you expect?, rather than being stuck in a never ending loop?
Re: For Loop with Random Never Stops!
When I do that the application just runs extremely slow and the label counter goes up very slowly. If I take those lines out, I'm back to getting files a gig or larger. RemSpace is now declared as an integer, just as i is. It shouldn't be taking that long to create the file with the label displaying the numbers, should it?
Re: For Loop with Random Never Stops!
The intention is to write RemSpace bytes to the file? You're actually writing RemSpace * (Bytes in your RandomNumber)
So to write "bytes" your random number needs to be 1 byte, so use;
RandomNumber = RandomClass.Next(0, 10).
But since this is not in your loop, you might was well write "0", since your not filling the file with random numbers, your just writing the same random number over and over again.
So if you just write "0", what happens?
Code:
For i As Integer = 1 To RemSpace
myStreamWriter.Write("0")
Next
You should end up with a file which is 118MB
Re: For Loop with Random Never Stops!
Quote:
Originally Posted by Bulldog
You should end up with a file which is 118MB
Wouldn't that be 236MB? You are writing the Ascii character "0", which is two bytes.
If he is writing an integer out (8 bytes), then the file would be 1GB in size.
Re: For Loop with Random Never Stops!
Re: For Loop with Random Never Stops!
Two bytes because it is unicode. ASCII would be one byte.
Re: For Loop with Random Never Stops!
Code:
Dim RandomClass As New Random()
Public Sub Create_DummyFile(ByVal remSpace As Integer)
Dim RandomNumber As Byte
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
For i As Integer = 1 To remSpace
RandomNumber = CByte(RandomClass.Next(0, 256))
myStreamWriter.BaseStream.WriteByte(RandomNumber)
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
End Sub