c# - CSV file creation is shorter than expected -
i'm creating csv file, i'm using c# , csvhelper so.
my csv file ends being 205 lines long each time. know should creating 240 entries, want.
it seems stop creating csv file @ 205 lines. when running several times produce same result of 205 entry's. interestingly last field attempts write 1 letter short , i'm not sure why.
below full code.
using system; using system.collections.generic; using newtonsoft.json; using csvhelper; using system.io; namespace pingenerator { class program { static void main(string[] args) { generatepins generatepins = new generatepins(); generatepins.start(); console.readline(); } } public class generatepins { private dictionary<string, string> pinnumbers = new dictionary<string, string>(); private list<string> duplicatepincheck = new list<string>(); private random random = new random(); // runs sequence generation of timespans & pin numbers. // stores these json / csv files public void start() { timespan timespan = new timespan(); timespan maxtime = timespan.fromhours(2); timespan currenttimespan = timespan.fromseconds(30); string currentpin = "0000"; // csv file creation textwriter textwriter = file.createtext(@"pinnumber.csv"); var csv = new csvwriter(textwriter); while (timespan.duration() < maxtime.duration()) { // create timespan & pin timespan = timespan.add(currenttimespan); currentpin = randomnumber(); // duplicate check while (duplicatepincheck.contains(currentpin)) { currentpin = randomnumber(); } duplicatepincheck.add(currentpin); // cache results string entrypinnumber = currentpin; string entrytimespan = timespan.tostring(); // json pinnumbers.add(entrytimespan, currentpin); // csv creation - write csv file csv.writefield(entrytimespan); csv.writefield(entrypinnumber); csv.nextrecord(); // console console.writeline(entrypinnumber + " / " + timespan.tostring()); } string json = jsonconvert.serializeobject(pinnumbers, formatting.indented); // write json file using (system.io.streamwriter file = file.createtext(@"pinnumbers.json")) { jsonserializer serializer = new jsonserializer(); serializer.serialize(file, pinnumbers); } console.writeline("created " + pinnumbers.count() + " pin numbers."); } public string randomnumber() { string result = random.next(0, 9999).tostring(); result = result.padleft(4, '0'); return result; } } }
textwriter textwriter = file.createtext(@"pinnumber.csv");
needed changed
streamwriter streamwriter = file.createtext(@"pinnumber.csv");
Comments
Post a Comment