Why isn't my file being read in C#? -
i'm not sure why file isn't being read in c# program. when run it, gives 0 in textbox , doesn't list names. wonder if problem else , reading file correctly. in advance everyone!
here customerda class calls file , i'm not sure if correct:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.io; namespace customerlist { public static class customerda { private const string dir = @"z:\desktop\windows 7 files\c#.net\customerlist\customerlist"; private const string path = dir + "customerdat.txt"; public static list<customer> getallcustomers() { if (!directory.exists(dir)) directory.createdirectory(dir); streamreader textin = new streamreader( new filestream(path, filemode.open, fileaccess.read)); list<customer> custlist = new list<customer>(); // create list add customers to. while (textin.peek() != -1) { string row = textin.readline(); string[] columns = row.split(','); customer customer = new customer(); customer.name = columns[0]; customer.email = columns[1]; customer.creditrating = convert.toint16(columns[2]); customer.city = columns[3]; customer.zip = columns[4]; } textin.close(); // use method read found in book example on page 677 // file read can found on moodle customerdat.txt // placed copy in project. //once of customers have been read list return list return custlist; } } }
here form1.cs, i'm not sure if calling correctly or not. seems correct me:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace customerlist { public partial class form1 : form { public form1() { initializecomponent(); } list<customer> ourcustomers = new list<customer>(); private void form1_load(object sender, eventargs e) { // call customerda class , read of customers file , return list here ourcustomers = customerda.getallcustomers(); } private void btnshowgood_click(object sender, eventargs e) { int numgoodcustomers = 0; // loop through of customers returned file foreach (customer cust in ourcustomers) { // see if customer has rating > 8 if(cust.creditrating > 8) { // add name listbox lstgoodcustomers.items.add(cust.name); // add 1 count of customers numgoodcustomers ++; } } // update form number of customers txtnumgoodcustomers.text = numgoodcustomers.tostring(); } } }
you never add list while reading, empty. add custlist.add(customer)
end of loop.
private const string dir = @"z:\desktop\windows 7 files\c#.net\customerlist\customerlist"; private const string path = path.combine(dir, "customerdat.txt"); list<customer> custlist = new list<customer>(); // create list add customers to. while (textin.peek() != -1) { string row = textin.readline(); string[] columns = row.split(','); customer customer = new customer(); customer.name = columns[0]; customer.email = columns[1]; customer.creditrating = convert.toint16(columns[2]); customer.city = columns[3]; customer.zip = columns[4]; custlist.add(customer); //this missing }
as @gusman noted (added answer completeness) need use path.combine
instead of '+' well, path invalid is.
Comments
Post a Comment