c# - Unexpected number of values when reading integers from a text file -


i reading integers text file int array, use following code. not seem read in expected number of integers:

string filecontent = file.readalltext("d:\\pixels.txt"); textwriter tw = new streamwriter("d:\\print.txt"); string[] integerstrings = filecontent.split(new char[] { ' ' }, stringsplitoptions.removeemptyentries); int[] integers = new int[integerstrings.length]; (int n = 0; n < integerstrings.length; n++) {     integers[n] = int.parse(integerstrings[n]);     tw.write(integers[n]+" "); } tw.write(integers.length+" "+integerstrings.length+" "); 

in file (tw) integers printed, integers.length , integerstrings.length not correct. should 262,144 both 41,616. cannot think of problems.

try this:

int dummyint; var integers = file.readalltext("c:\\temp\\pixels.txt")                    .split(new char[] { ' ', '\r', '\n' })                    .select(n => (int?)(int32.tryparse(n, out dummyint)                               ? dummyint                               : (int?)null))                    .where(n => n.hasvalue)                    .select(n => n.value)                    .toarray(); 

the above code should give array of integers read input file, split ' ' , new line , select these values can converted int32.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -