How to ignore blank lines from TextFile when they are between and after the lines?
Mistake
TextFile format:

2016-01-01 20:23;Work;5;чистка;5000.00 2016-01-03 00:40;Custom;1;2;6;3 2016-01-03 00:41;Custom;1;1;10;1 

48 line if (element[1] == "Card")

But the code itself:

 public void SplitAndQuery(string path) { char[] separatorLines = { '\n' }; char[] separatorWords = { ';' }; string[] lines = File.ReadAllText(path, Encoding.Default).Split(separatorLines, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i <= lines.Length - 1; i++) { string[] element = lines[i].Split(separatorWords, StringSplitOptions.RemoveEmptyEntries); if (element[1] == "Card") { if (checkCard(element[2]) == false) { insertCard(element[2], element[3], element[4], element[5], element[6], element[7], element[8]); if (checkDiscount(element[2]) == "-1") { connection.Close(); MessageBox.Show("ups"); } else { insertDiscount(element[0],element[9]); } } else if (checkCard(element[2]) == true) { updateCard(element[3], element[4], element[5], element[6], element[7], element[8], element[2]); if (checkDiscount(element[2]) == "-1") { connection.Close(); MessageBox.Show("ups"); } else { insertDiscount(element[0],element[9]); connection.Close(); } } } 
  • one
    the problem is not in empty lines, but in the fact that the lines are not the same number of words , some have element [5], element [6] some do not exist, and in this place everything drops - Grundy
  • Not all TextFile displayed. Everything works when there are no empty lines in TextFile. As soon as I add to the end of the file \ n then the program does not work. - YoFi
  • one
    it doesn't matter, as soon as a line comes in which there are few elements, and you try to get it, as if it has a lot - you get your mistake - Grundy
  • one
    By the way, it would not be bad to mark line 48, which is indicated in the error - Grundy
  • Try this char[] separatorLines = { '\r', '\n' }; - Dmitry

3 answers 3

Some operating systems, such as Microsoft Windows, use a combination of CR carriage return codes ( \r ) and LF line feed ( \n ) as a line feed. To correct the example given in the question, it is enough to add the symbol \r

 char[] separatorLines = { '\r', '\n' }; 

More correct option, in my opinion, would be to use Environment.NewLine For example, so

 char[] separatorLines = Environment.NewLine.ToCharArray(); 

    try adding verification

     string.IsNullOrEmpty(lines[i]){ return; } 

    right after

      for (int i = 0; i <= lines.Length - 1; i++) { 
    • one
      Instead of return; probably better continue; - Dmitry
    1. Reading all the lines from a text file is bad. Why? Google
    2. If you know the exact data structure, it’s better to use regular expressions.

    In your particular question, the error in the 48 line arises because you are trying to divide the empty line. The Split method if it does not find a separator in the string will return an array with 1 element - the original string. And then when you call on index 2, you will go beyond the array. To avoid this situation, simply check the number of elements in the array after Split by the Length property.