c# - int.TryParse returning false on the first line of data from StreamReader -


long time reader, first time poster.

i'm reading numbers text file integer array, 1 integer per line, using streamreader class. read through file once number of lines can define destination array. reset pointer beginning of text file , read again, time using int.tryparse covert strings integers , write array. first line file returns 'false' tryparse - though string merely '3. subsequent lines return 'true' fine. here's snippet ...

        _numbersinmemory = new int[linecount];          thefilestream.discardbuffereddata();         thefilestream.basestream.seek(0, 0);          linecount = 0;                 {             string thelinefromthefle = thefilestream.readline();             int numberinmemorytemporarily = 0;             bool result = int.tryparse(thelinefromthefle, out numberinmemorytemporarily);             if (result)             {                 _numbersinmemory[linecount] = numberinmemorytemporarily;             }             linecount++;         } while (!thefilestream.endofstream);          thestream.close(); 

is reset beginning of file (which 82 lines) messing-up input first iteration of tryparse, or that?

your file has utf8 or unicode byte-order-marks (bom) @ start of file. looking @ file in hex-viewer can verify this.

when first open file, streamreader class reads these bytes determine proper encoding of rest of file. first readline() not include these bytes.

by seeking byte 0 of underlying stream, next readline() include these bytes because streamreader not know should skip them (it's read , processed them).

either read file once using readalllines, or close , re-open file start reading again rather seeking beginning.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -