c# - Reading textfile skipping bunch of lines then proceeding to other lines -
i want skip lines read in text file, can see below line 0, line 1 , line 2 skip (first 3 lines of text file) follows read (3 read this) next (4 skip this)
this the pattern:
(first 3 lines of text file skipped followed (3) read (4) skip till end of text file) rinse , repeat other text files, need kind of pattern.
line 0 - skip line 1 - skip line 2 - skip line 3 - read line 4 - read line 5 - read line 6 - skip line 7 - skip line 8 - skip line 9 - skip line 10 - read line 11 - read line 12- read line 13 - skip line 14 - skip line 15- skip line 16- skip line 17 - read , on code tried.
string[] lines = file.readalllines(f.file); foreach (string line in lines.skip(3)) { try { string[] readlinesplit = line.split('|'); if (readlinesplit.length > 1) { var id = readlinesplit[0].tostring(); var flagvalue = readlinesplit[1].tostring(); var status = readlinesplit[2].tostring(); lines.skip(4).tostring(); console.writeline("id {0}, value {1}, status {2}", id, flagvalue, status) } } catch (filenotfoundexception error) { console.writeline(error); } }
rather reading in lines , attempting extract ones need, can instead read lines want without storing unneeded ones in memory:
var lines = new list<string>(); var counter = 0; using (var sr = new streamreader(filename)) { string line; while ((line = sr.readline()) != null) { //ignore first 3 lines if (counter >= 3) { //now work out in pattern of 7 (3 read, 4 skip) var mod = (counter - 3) % 7; //if first 3 of 7, keep if (mod < 3) lines.add(line); } counter++; } }
Comments
Post a Comment