Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everybody,

I have this:

C#
static void Main(string[] args)
    {


        const int linesPerFile = 10;
        const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
        //string fileName = "File";






            var fileCounter = 0;
            var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
            try
            {

                // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                //{
                var lineCounter = 0;
                string line;

                while ((line = destiNationFile.NewLine) != null)
                {
                    foreach (var bank in BankAcoutNumbers.BANS.Take(200))
                    {
                        if (lineCounter >= linesPerFile)
                        {
                            lineCounter = 0;
                            fileCounter++;
                            destiNationFile.Dispose();
                            destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));

                        }

                        destiNationFile.WriteLine(bank);
                        lineCounter++;
                    }
                    //}


                }
            }
            catch (Exception)
            {

                throw;
            }



        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }


But the problem is it creates endless files. Not 20 files - what it has to be.

Thank you

I have it now like this:


C#
static void Main(string[] args)
    {


        const int linesPerFile = 10;
        const string destinationFileName = @"G:\Folder\File-Part-{0}.txt";
        //string fileName = "File";
        var maxNumberOfFiles = 10;
        Stopwatch timer = new Stopwatch();



            var fileCounter = 0;

            var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
            try
            {

                // foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                //{
                var lineCounter = 0;
                string line;

                while (fileCounter <= maxNumberOfFiles)
                {
                    timer.Start();
                    foreach (var bank in BankAcoutNumbers.BANS.Take(100))
                    {
                        if (lineCounter % linesPerFile == 0)
                        {
                            //lineCounter = 0;
                            destiNationFile.Flush();
                            destiNationFile.Dispose();
                            destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter + 1));
                            fileCounter++;
                        }

                        destiNationFile.WriteLine(bank);
                        lineCounter++;

                    }

                    fileCounter++;

                    //}


                }
                timer.Stop();
                Console.WriteLine(timer.Elapsed.Seconds);
            }
            catch (Exception)
            {

                throw;
            }



        // Keep the console window open in debug mode.

        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }

But I get ten fiels, so that is correct 10*10. But the problem is that every time the tinth file is empty
Posted
Updated 20-Jan-15 2:37am
v2

The problem is with your while loop condition, it never gets to false so never stops

You should put something that eventually evaluates to false, something like

C#
var maxNumberOfFiles= 20

while (fileCounter < maxNumberOfFiles) {

//your code here

fileCounter++;

}
 
Share this answer
 
That's becuase you have an assignment operation as your sentinel, and assignments always return true. Assignments in sentinals are a giant anti-pattern

I suspect you're trying to do this:

C#
var fileCounter = 1;
var lineCounter = 0
var destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter));
foreach(var bank in BankAcoutNumbers.BANS.Take(200))
{
    if(lineCounter % linesPerFile == 0)
    {
        destiNationFile.Flush();
        destiNationFile.Dispose();
        destiNationFile = new StreamWriter(string.Format(destinationFileName, fileCounter));
        fileCounter++;
    } 
    destiNationFile.WriteLine(bank);
    lineCounter++;
} 


Oh, and then you dispose of it in the loop, which is bad juju since your break condition now no longer exists in memory. Use a recursive function for this, not a while loop.

The while loop looks to be meaningless, so here's a more concise copy.
 
Share this answer
 
v4
Comments
[no name] 20-Jan-15 7:39am    
But it is StreamWriter I cant use ReadLine
Nathan Minier 20-Jan-15 7:44am    
Oh wow, Monday(or day after holiday) mornings are awful.

The basic principal is still true, though. You're making an assignment.

You need to reconsider your design, since the nested structure you're using is common for reading files and selective writing, but then I might just need more coffee.

Going in to correct answer now.
[no name] 20-Jan-15 8:05am    
Ok, will be nice to see your solution :)
[no name] 20-Jan-15 8:38am    
Nice solution. I have updated my question. See above.
Nathan Minier 22-Jan-15 8:14am    
Yeah, there needs to be a terminal Flush and Dispose, that's likely the problem, and was not in my code block.

It doesn't thrill me since it's not terribly DRY, but it is a quick answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900