file io - Random lines not being written from streamwriter C# -
i working on fixing issue legacy code. code reads csv, checks data validity, writes document outfile.
i having difficulty determining reason process being hung up. stuck in infinite loop. issue happens on same line every time (line 262). have removed line , runs line 1885 because getting hung again. continued remove lines causing issues on file , test file. weird errorline file ran fine when in file. makes me think issue must coming streamwriter.
i have streamwriter set autoflush = true
. using using
block should handle flush/close
of streamwriter.
the files come in number of different sizes, handful of rows , several thousand rows.
foreach (string e in filenames) { using (streamwriter sw = new streamwriter(e.replace(inputdirectory, outputdirectory), false, encoding.unicode)) { sw.autoflush = true; using (streamreader sr = new streamreader(e,encoding.unicode)) { int peek = sr.peek(); while (peek > -1) { if (((char)peek).equals(textqual)) { sw.write(((char)sr.read())); peek = sr.peek(); if (((char)peek).equals(textqual2)) { sw.write(((char)sr.read())); peek = sr.peek(); while (peek > 1) { if (((char)peek).equals((char)0x0a) || ((char)peek).equals((char)0x0d)) { check = false; row++; if (firstcol == 0) { firstcol = col; } col = 0; break; } else if (((char)peek).equals(del)) { check = false; break; } else if (((char)peek).equals((char)0x22)) { sr.read(); doubleq++; } else { sw.write(((char)sr.read())); peek = sr.peek(); break; } } } }
i looking suggestions issue not sure coming from.
you in infinite loop due while (peek > -1) wich evaluate true (if strream contains @ least 1 character).
correct code this:
int peek = 0; while ( (peek = sr.peek ()) > -1) { char c = (char)peek; // doe whatever want picked character sr.read (); }
in oppinion dangerous code because there cases when while can run forever. example, if statement:
if (((char)peek).equals(textqual))
above, if first character not equal textqual while run forever. not mentioning can happen characters read further.
above piece of code wouldn't recommend put in production.
Comments
Post a Comment