Gigi Labs

Please follow Gigi Labs for the latest articles.

Tuesday, May 7, 2013

C#: Creating a grep-like tool using Files, Strings and Loops

Hello again! :)

Today we're going to learn to read files on the hard disk, and also some other interesting stuff that everybody obviously dreams of learning at some point in their lives, including loops and arrays.

Start a new SharpDevelop project, and right under the using System;, put in the following:

using System.IO;

What we're doing here is enabling the use of stuff related to input and output (I/O) in our program - in our case, files fall in that category.

When you learn C#, you don't just learn the language. There exist tons of functionality within this thing called the .NET Framework. System.IO is but one small part of this framework.

Great... two sentences of philosophy are enough. Let's get our hands dirty. Create a file called chat.txt anywhere you like, and put the following text in it:

Me: Hi!
John: Hello Daniel!
Me: So, uh... what's up?
John: Nothing much.
Me: Well, in my case, I ate loads of pastizzi yesterday, and I feel I might explode.
John: Great, you might take down the Luqa monument in doing so.
Me: Thanks, I'm flattered by your concern.
John: What are pastizzi anyway?
Me: The Maltese fast food. Very good, very unhealthy.

Now, drag that file into the Project panel on the right hand side of SharpDevelop. With the file selected in SharpDevelop, press F4 to bring up the Properties window, and set "Copy to Output Directory" to "Always". The next time you press F5, chat.txt will be copied to the same directory as the executable.

All we need to do to read a file is the following:

            String fileContents = File.ReadAllText("chat.txt");

We are reading the entire file into a String object and then writing it to the console window. This is what the result looks like:

That's easy. Let's do something a bit more advanced. Scrap the code you have so far, and instead, let's do this:

             String[] lines = File.ReadAllLines("chat.txt");

What we're doing here is reading each line into an array of Strings, which is what String[] means. The lines variable is not just a simple String; you can think of it as something like this:

An array is a sort of list, but it has a fixed size, and each item in the list is numbered, starting at zero. If you read yesterday's article, C# Basics: Working with Strings, this will sound familiar. In fact, a String is internally made up of an array of characters! We can access each item in this array just like how we accessed individual characters within a String:

             String firstLine = lines[0];

The example above gives you the first line, "Me: Hi!".

We can iterate over the array using a foreach statement:

            String[] lines = File.ReadAllLines("chat.txt");
            foreach (String line in lines)
            Console.Write("Press any key to continue . . . ");

This effectively gives us the same thing as the first example in this article, because we're going over each line in the lines array, storing it in a variable called line, and writing it to the console window:

Now, we can make this more interesting by writing only those lines that contain the word "pastizzi". The Contains() method, available to all Strings, allows us to check if a String contains another String. This is the complete code:

            String[] lines = File.ReadAllLines("chat.txt");
            foreach (String line in lines)
                if (line.Contains("pastizzi"))
            Console.Write("Press any key to continue . . . ");

...and the output, as expected:

This simple functionality is useful when you want to find some particular text in a large file (searching for a needle in a haystack). In fact, if you've used Linux, you'll know that the grep tool does exactly that:

Excellent! After just a few articles, we're already able to create useful tools with a minimal amount of code. But don't go away... things are only going to get better!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.