Length space as a condition for the programme
-
The code below reads the lines in the text file and converts into a body. That's understandable. But I can't stop at that. Condition to be fulfilled for the continuation of work - Exactly. Six lines in the file. How should it be:
6 lines - programme ongoing
* Less than six lines - the program gives a wrong window and finishes the job.using (System.IO.StreamReader sr = new System.IO.StreamReader(f.FullName, Encoding.GetEncoding("windows-1251"))) { string str = ""; string s = ""; while ((s = sr.ReadLine()) != null) { str += s + "@";//делим текст при помощи собаки на строки } string[] split = str.Split('@'); int counter = split.Length; if (counter == 6) { //действия } else { //выход из приложения }
In my case, the program always ends with a mistake, even if I put a "slower or equal to six." Line in file six, checked. I don't know what I'm doing wrong. The text has been translated into a mass, Length is the nature of the mass, a whole number. What's the mistake?
-
How hard is it? It's C#, it doesn't have to be tortured.
Do that.
var lines = File.GetLines(f.FullName, Encoding.GetEncoding("windows-1251")); var first6lines = lines.Take(6).ToList(); if (first6lines.Count < 6) { // ... то выдать сообщение об ошибке и свалить } // а дальше проходитесь циклом по строкам и делайте с ними, что хотите