SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast xarzu's Avatar
    Join Date
    Apr 2007
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Organize Data Read From A File?

    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:
    Code:
                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)?

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,629
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    A naieve implementation would use string.Split('\t'). That said, check out filehelpers -- it is pretty much all you need to process a delimited or fixed-width text file.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Location
    OH, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use LINQ to load data to list and manipulate the way you need...like
    getList = from line in File.ReadAllLines(fileName)
    let itemRecord = line.Split('\t')
    select new Record()
    {
    itemID = itemRecord [0],
    lname = itemRecord [1]
    }.ToList();

    Make sure you declare a class Record with properties itemIDand lname

  4. #4
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    This can be done natively with the Odbc system. From the MSDN archive:

    Code Csharp:
    public DataSet SelectOdbcSrvRows(DataSet dataset,string connection,string query) 
    {
        OdbcConnection conn = new OdbcConnection(connection);
        OdbcDataAdapter adapter = new OdbcDataAdapter();
        adapter.SelectCommand = new OdbcCommand(query, conn);
        adapter.Fill(dataset);
        return dataset;
    }

    As long as you know how to set up an odbc connection string, you're golden. Here's an example:


    string connection = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:;Extensions=csv,txt";

    Here is some further reading for you.

    http://msdn.microsoft.com/en-us/library/bbw6zyha.aspx

    I hope this helps. =)

    * The sample code does not demonstrate proper open/close/dispose practices. It is provided as a sample only.
    Last edited by Serenarules; Aug 16, 2012 at 02:27. Reason: Added additional comments.

  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Regards,
    Sabareesh
    Last edited by Stevie D; Sep 8, 2012 at 07:26. Reason: Fake signature removed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •