1

This is something similar from what I am working on, I have to do the 3 files at the same time sequentially, and when string is found it displays the data from that line of each file. My problem so far is that it does not read data from the second and third files correctly, it reads only the first line. My second problem, I am trying to use

String.Compare(string1, searchBox, true) 

but I am not really sure on where to put it so the search ignores spaces (trim) or capital letters during search. The code:

    string string1, string2, string3, searchBox;

    StreamReader file1, file2, file3;

    file1 = File.OpenText("data1.dat");
    file2 = File.OpenText("data2.dat");
    file3 = File.OpenText("data3.dat");

    string1 = file1.ReadLine();
    string2 = file2.ReadLine();
    string3 = file3.ReadLine();

    searchBox = searchTxtBox.Text.Trim();

    while ((string1 = file1.ReadLine()) != null)
    {
          if (string1.Contains(searchBox))
          {
           infoListBox.Items.Add(string1 + "====" + string2 + "====" + string3);
           break;
          }
    }

file1.Close();
file2.Close();
file3.Close();

1 Answers1

2
string string1, string2, string3, searchBox;

StreamReader file1, file2, file3;

file1 = File.OpenText("data1.dat");
file2 = File.OpenText("data2.dat");
file3 = File.OpenText("data3.dat");

searchBox = searchTxtBox.Text.Trim();

while ((string1 = file1.ReadLine()) != null
    && (string2 = file2.ReadLine()) != null 
    && (string3 = file3.ReadLine()) != null)
{
      if (string1.IndexOf(searchBox, StringComparison.CurrentCultureIgnoreCase) >= 0)
      {
       infoListBox.Items.Add(string1 + "====" + string2 + "====" + string3);
       break;
      }
}

file1.Close();
file2.Close();
file3.Close();
NoName
  • 7,940
  • 13
  • 56
  • 108
  • Thank you! it finally updated the second and third files with the && and StringComparison – Maximilious Feb 21 '16 at 04:22
  • 1
    Great! Glad it help you! – NoName Feb 21 '16 at 04:28
  • do you know why I am getting "cannot implicitly convert type 'string' to 'decimal' for && (string2 = file2.ReadLine()) != null) I made sure to convert to decimal using string2 = decimal.Parse(file2.ReadLine()) – Maximilious Feb 21 '16 at 04:55
  • Are you sure you data can convert to decimal? Try use `decimal.TryParse` instead. – NoName Feb 21 '16 at 05:00
  • 1
    Dont use `decimal.Parse(file2.ReadLine())` because you dont check for null before pass string to `Parse` method – NoName Feb 21 '16 at 05:04
  • the file2 includes a list of "sales numbers" which some are "corrupted" example: 35 6f.30 most of the numbers are perfect except for a few "corrupted" I try to use TryParse but I get cannot implicitly convert type of 'bool to 'decimal' I have gameSales = decimal.TryParse(saleFiles.ReadLine(), out gameSales); – Maximilious Feb 21 '16 at 05:07
  • 1
    `gameSales = decimal.TryParse(saleFiles.ReadLine(), out gameSales);` is wrong, use `if (decimal.TryParse(saleFiles.ReadLine(), out gameSales))` is true if it can convert, otherwise, false – NoName Feb 21 '16 at 05:09