Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Organize Data From A File

Back to Forum: Tech Off
  • User profile image
    complete

    There are some cool classes and tricks and short cuts in C#.  I know this but I have not worked with C# enough to have them memoried.

    One cool trick is this.  If you read from a file data that is arranged in a similar line-by-line structure where each line is arranged in columns spaced out by tabs, the data can be read into a kind of class in C# and then you can parse out one column of data by simply doing a "for each" command.  Does anyone know off-hand how this is done?

    Basically, what I am talking about is this.  I understand how to read a file line by line:

                int counter = 0;        
                string line;
                 System.IO.StreamReader file =
                    new System.IO.StreamReader(filename);
                while ((line = file.ReadLine()) != null)
                 {
                     Console.WriteLine(line);
                     counter++;
                 }
                file.Close();

    Now, how would I go about replacing Console.WriteLine(line); with code that will organize the data automatically into members of a class provided that the line columns in the input file are seperated by /t (tabs)?

  • User profile image
    JohnAskew

    You can experiment with the String.Split() method, where the delimiter (tab or comma) is an argument. This method returns a String array of your columns. Once you've chopped your line into columns, you can foreach over them.

     

  • User profile image
    spivonious

    @JohnAskew:Yep, that's how I'd do it too.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.