-3
private void Button4_Click(object sender, EventArgs e) {
 string start = Directory.GetCurrentDirectory() + @"\Sav1.txt";

 using(var streamReader = new StreamReader(start)) {
  string line = streamReader.ReadLine();

  int[] values = line.Split(' ').Select(int.Parse).ToArray();

  Array.Sort(values);

  Array.Reverse(values);

  for (int i = 0; i < values.Length; i++) {
   richTextBox4.AppendText(values[i] + " ");
  }
 }
}

Hey guys I need more information about this code. Could anyone explain me how it works. I mostly googled it so dont know how it works. Also I have added .txt file and got access to first row, but i need to get into 7 and 8 row. There's just random numbers in that .txt file.

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
Tom
  • 5
  • 2
  • Have you tried stepping through the code with a debugger? How about copying and pasting the classes, methods and properties into an internet search tool. The names of things (like `Directory.GetCurrentDirectory` and `Array.Sort`) seem pretty obvious. – Flydog57 Dec 01 '19 at 18:30
  • Which part of the code specifically don't you understand? – Sweeper Dec 01 '19 at 18:34
  • Hey, Yeah code works perfectly, but i just get access to first row of .txt file. I understand some of it. Only dont know about using(var streamReader = new StreamReader(start)) { string line = streamReader.ReadLine(); int[] values = line.Split(' ').Select(int.Parse).ToArray(); – Tom Dec 01 '19 at 18:34
  • @Sweeper Yeah idk how does using works why i need to do that new StreamReader() – Tom Dec 01 '19 at 18:40
  • @Tom See [this](https://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it). – Sweeper Dec 01 '19 at 18:41

2 Answers2

1

Ok so there's a better way to do this. StreamReader has a .Peek() method that is extremely useful for reading to the end of a file. Basically the only thing you're missing is a way to loop through each line in your file.

Start with your using statement.

   using(var streamReader = new StreamReader(start)) {...
   //Using statement is a way for you to close your stream after the work
   //is completed. This is why you need to get a loop inside of here.

Use a loop to read to the end of a file.

This part is what I'd change in your code. Your Using statement will not loop. You have to tell it to loop. Peek() returns -1 or the next character to be read. So you can use this to your advantage to check if you want to read to the end of file. Review the documentation here. https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.peek?view=netframework-4.8

while(streamReader.Peek() > -1)
    {...

Then you can split to your array per line by simply reading in each or splitting to a character.

int[] values = streamReader.Readline().Split(' ');

Finally you can do the remainder of your code.

for (int i = 0; i < values.Length; i++) {
    richTextBox4.AppendText(values[i] + " ");
}

To break down exactly what is going on in your code. Read each comment line by line to get a basic understanding.

//Button Click Event
private void Button4_Click(object sender, EventArgs e) {

//Finding your file and assigning it as a string. 
 string start = Directory.GetCurrentDirectory() + @"\Sav1.txt";

//Using a `using` statement and creating a new streamReader that will look
//for the string that you created above to find the file. The using block
//will properly close the streamreader when the work is done. 
 using(var streamReader = new StreamReader(start)) {

//StreamReader.ReadLine() will read the FIRST line, no loop here.
//This is why your code only reads one.
  string line = streamReader.ReadLine();

//Splitting to an array. 
  int[] values = line.Split(' ').Select(int.Parse).ToArray();

//Sorting array.
  Array.Sort(values);

//Reversing the array. 
  Array.Reverse(values);

//Looping through the array based on count. 
  for (int i = 0; i < values.Length; i++) {
   richTextBox4.AppendText(values[i] + " ");
  }
 }
}

If you're attempting to only get rows 7, 8, 9, etc.. within your for loop at the very end of your code do the following.

//Looping through the array based on count. 
      for (int i = 0; i < values.Length; i++) {
       //This will only append rows 7-9. Keep in mind an array is a 0 based
       //index. Meaning row 7 is actually array element 6. Row 9 is actually
       //array element 8. 
       if(i > 5 && i < 9){
           richTextBox4.AppendText(values[i] + " ");
       }
      }
     }
    }
xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
  • Hey @xTwisteDx thnx for help, I got one more question. So i did like u said added that while (streamReader.Peek() > -1) {... and i debugged all .text i got. But i only need 7-8-9 rows. Tried some thing but didn't worked. – Tom Dec 01 '19 at 21:04
  • thanks a lot xTwisteDx ! – Tom Dec 02 '19 at 15:34
  • @Tom please take the time to mark my answer as the correct answer if I helped. I'm trying to boost my reputation on the site. If it wasn't sufficient, please let me know so I can update my answer. – xTwisteDx Dec 02 '19 at 20:47
0

So my current code looks like this:

private void Button4_Click(object sender, EventArgs e) 
        {
            string start = Directory.GetCurrentDirectory() + @"\Sav1.txt";

            using (var streamReader = new StreamReader(start))
            {
                while (streamReader.Peek() > -1)
                {

                    string[] values = streamReader.ReadLine().Split(' ');

                    Array.Sort(values);

                    Array.Reverse(values);

                    for (int i = 0; i < values.Length; i++)
                    {
                        richTextBox4.AppendText(values[i] + " ");
                    }
                }
            }
Tom
  • 5
  • 2