An explanation of ReadFile()

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

I was just writing some code that would connect to a file in the directory of the application and I came across this line of code and I was curious to know what it does.

I can understand that we started by checking to see if the file path exists, then we go to say by using the streamer create a new path. Then we are saying that while Sr is not equal to the end of the stream the result should Sr ReadLine().

This is where I got very confused after (after the While statement)

Here is my code:


public void ReadFile()
        {
            List<Staff> myStaff = new List<Staff>();
            string[] Result = new string[2];
            string Path = "Staff.txt";
            string[] Separator = { ", " };


            if (File.Exists(Path))
            {
                using (StreamReader sr = new StreamReader(Path))
                {
                    while (!sr.EndOfStream)
                    {
                        Result = sr.ReadLine().Split(Separator, StringSplitOptions.RemoveEmptyEntries);
                    }
                } 
            }



The code above was written in a class.

I hope this is clear and easy to understand.

Cheers,
 
I can understand that we started by checking to see if the file path exists,
Yes.
then we go to say by using the streamer create a new path.
No. There's no new path created. We already have the path and have confirmed that a file with that path exists. That line is opening the file at that path and creating a new StreamReader, which is an object that will read text from a Stream. In this case, the Stream will be a FileStream, because we're reading text from a file. The 'using' statement is used so that the StreamReader object will be disposed implicitly at the end of the code block, which will close the file.
Then we are saying that while Sr is not equal to the end of the stream the result should Sr ReadLine().
The EndOfStream property will be true when you have reached the end of the Stream that you're reading and false otherwise, so that loop basically says keep reading text until you get to the end of the file. The ReadLine method reads a line of text, so that code is reading the file line by line to the end. That Split call is going to split the line just read on any commas it contains and discard any empty results. That means that if you read a line that contained "First,Second,Third" then you'd get an array containing {"First", "Second", "Third"} and if you read a line containing "First,,Third" then you'd get an array containing {"First", "Third"}.

The odd thing about that code is that it assigns the result of reading a line and splitting it up to a variable named 'Result' and then just goes right on to the next line without using that result. That means that none of the text in the file is actually ever going to be used by that code. After that code executes, the Result variable still contains the data from the last line of the file, so that data could still be used.
 
Hey, thanks for a detailed answer!

By the way, was that question asked in a better format? I hope it's an improvement on the previous question.

I now understand what the code does, thank you for that!

But to answer your question: We are basically making a simple payroll software in the console and we are doing this by making multiple classes with fields, properties, constructors and methods. Then we will be finally making use of them in the Main () method at the end.

Also, after I have covered this book, are there any books that you would suggest that would teach more advanced things? Most books cover variables, classes and other things. But is there anything that would challenge you to learn even more? I would interested to hear your suggestions.

Cheers,
 
Back
Top Bottom