Reading Data from a file in C#

401
1
08-12-2012 09:02 PM
Labels (1)
wmthompson
New Contributor
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?
0 Kudos
1 Reply
AlexanderMagnusson
New Contributor
Hello

I think this entry on StackOverflow should provide some help:
http://stackoverflow.com/questions/1028224/c-sharp-read-text-file-containing-data-delimited-by-tabs

To give a brief outline on a simple scheme, suppose you have a file containing the following (\t indicates a tab)
MyClass\tValue1\Value2


You would then read a line from the file, split it into its constituent elements. Then you could send these elements as arguments to a function "createObjectFromText" that looks at the first argument ("MyClass"), decides what object should be created, and then creates such an object from the other parameters.

I hope this provides some guidance on creating objects from a textual source.
0 Kudos