.net - Read All Lines in a Text File and Add Them C# -
i've been working on problem while , i'm little stuck. have text file need loop through , read lines of, add of substrings 1 final number. problem is, have reading correctly , producing number first line in file only. i'm not sure whether use 'while' or 'for each'. here code have:
string filepath = configurationsettings.appsettings["benefitsfile"]; streamreader reader = null; filestream fs = null; try { //read file , estimated return. fs = new filestream(filepath, filemode.open, fileaccess.read, fileshare.readwrite); reader = new streamreader(fs); string line = reader.readline(); int soldtodate = convert.toint32(convert.todouble(line.substring(10, 15))); int currentreturn = convert.toint32(soldtodate * .225); //update return amount updatecurrentreturn(currentreturn);
any suggestions appreciated.
you use while loop so, reading in each line , checking see hasn't returned null
string filepath = configurationsettings.appsettings["benefitsfile"]; streamreader reader = null; filestream fs = null; try { //read file , estimated return. fs = new filestream(filepath, filemode.open, fileaccess.read, fileshare.readwrite); reader = new streamreader(fs); string line; int currentreturn = 0; while ((line = reader.readline()) != null){ int soldtodate = convert.toint32(convert.todouble(line.substring(10, 15))); currentreturn += convert.toint32(soldtodate * .225); } //update return amount updatecurrentreturn(currentreturn); } catch (ioexception e){ // handle exception and/or rethrow }
Comments
Post a Comment