0

I want to create a list with all the numbers between 0000000 - 9999999 and to create a new file every 50000 rows.

I tried this code:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\numbers" + fileparam + ".txt"))
{
    for (counter = 0 ; counter < 10000000 ; counter++)
    {
        if (counter < 10)
        {
            file.WriteLine("000000" + counter.ToString());
        }
        else if (counter > 9 && counter < 100)
        {
            file.WriteLine("00000" + counter.ToString());
        }
        else if (counter > 99 && counter < 1000)
        {
            file.WriteLine("0000" + counter.ToString());
        }
        else if (counter > 999 && counter < 10000)
        {
            file.WriteLine("000" + counter.ToString());
        }
        else if (counter > 9999 && counter < 100000)
        {
            file.WriteLine("00" + counter.ToString());
        }
        else if (counter > 99999 && counter < 1000000)
        {
            file.WriteLine("0" + counter.ToString());
        }
        else if (counter > 999999 && counter < 10000000)
        {
            file.WriteLine(counter.ToString());
        }
        if (counter % 50000 == 0)
        {
            fileparam++;
        }
    }
}

but it didn't work, because it already opened with the "fileparam" that was initialized at the start of the program "int fileparam = 0;" and i got "numbers0.txt"

I also tried this:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"E:\numbers" + fileparam + ".txt"))
{
    for (counter = 0 ; counter < 10000000 ; counter++)
    {
        if (counter < 10)
        {
            file.WriteLine("000000" + counter.ToString());
        }
        else if (counter > 9 && counter < 100)
        {
            file.WriteLine("00000" + counter.ToString());
        }
        else if (counter > 99 && counter < 1000)
        {
            file.WriteLine("0000" + counter.ToString());
        }
        else if (counter > 999 && counter < 10000)
        {
            file.WriteLine("000" + counter.ToString());
        }
        else if (counter > 9999 && counter < 100000)
        {
            file.WriteLine("00" + counter.ToString());
        }
        else if (counter > 99999 && counter < 1000000)
        {
            file.WriteLine("0" + counter.ToString());
        }
        else if (counter > 999999 && counter < 10000000)
        {
            using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"E:\numbers" + fileparam + ".txt"))
            {
                file.WriteLine(counter.ToString());
            }
        }
        if (counter % 50000 == 0)
        {
            fileparam++;
        }
    }

and all the files after numbers0.txt were empty.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
YossiH
  • 17
  • 5
  • This doesn't answer your questiob but you don't need all those if statements. You can pad 0's to your number like this `file.WriteLine(counter.ToString().PadLeft(7,'0'))` or `file.WriteLine(counter.ToString("0000000"))` – Loocid May 01 '15 at 05:55
  • 1
    Is this what you *actually* need to do, or is your real work doing something different in terms of the data? The exact approach I'd take would depend on the situation - you may be able to "batch" the data, collecting everything you need to write to each file separately, then iterate over that "collection of collections", writing one file per collection. – Jon Skeet May 01 '15 at 05:57
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 01 '15 at 06:27

1 Answers1

1

You can try this:

var counter = 0;
var fileParam = 0;
StreamWriter sw = new StreamWriter(string.Format("C://archive/Numbers/numbers{0}.txt", fileParam));

while (counter < 10000000)
{
    sw.WriteLine(counter.ToString("D7"));
    if ((counter + 1) % 50000 == 0)
    {
        fileParam++;
        sw.Flush();
        sw.Close();
        sw = new StreamWriter(string.Format("C://archive/Numbers/numbers{0}.txt", fileParam));
    }
    counter++;
}

sw.Flush();
sw.Close();
Paolo Costa
  • 1,989
  • 1
  • 12
  • 15
  • 1
    Nice, but I can offer a couple of improvements: first, use counter.ToString with a format instead of creating new strings: counter.ToString("D6") [(see this)](http://stackoverflow.com/questions/4325267/c-sharp-convert-int-to-string-with-padding-zeros). Second, instead of writing the strings one by one into the StreamWriter I think a better solution in this case will be to use a StringBuilter and write to the file using File.WriteAllText on each fileParam change.([read here])(http://stackoverflow.com/questions/17988457/streamwriter-vs-stringbuilder) I admit I'm not sure about the second one. – Zohar Peled May 01 '15 at 06:45
  • Thanks, i was not aware of that format option. I updated my answer. I don't know whether a StringBuilder is better, I'll do a performance test and let you know, you are probably right :-) – Paolo Costa May 01 '15 at 06:59
  • almost no difference, stream: 12147 ms, StringBuilder 12902 ms – Paolo Costa May 01 '15 at 07:08