Roy Ashbrook

let go or be dragged
posts - 198, comments - 29, trackbacks - 8

Showing status in c# loop using the modulus operator (%)

I needed to take some records in one file, do some stuff with the record, then put the results in another file. The 'do stuff' part was taking a bit longer than I thought it would so I wanted to see status. This sample illustrates taking 1000 records from a source file, uppercasing each line, putting it in another file, and letting us know every 100 lines.

using(TextWriter tw = new StreamWriter(@"uppercase.txt",false))

{

    int i = 0;

    var q = File.ReadAllLines(@"lowercase.txt").Take(1000);

    foreach(var v in q)

    {

        tw.WriteLine("{0}", v.ToUpper());

//print status on every 100th record

        if ((i % 100) == 0) Console.WriteLine("{0}/{1} processed", i.ToString(),q.Count().ToString());

        i++;

    }

    Console.WriteLine("{0}/{1} processed", q.Count(),q.Count().ToString());

}

Print | posted on Friday, June 25, 2010 9:08 AM |

Comments have been closed on this topic.

Powered by: