0

I need to read text data from file, where there are different types of data in every line. So, I created a one big class named subject. My data looks something like this:

Subject name      M1   M2   M3   M4
Subject1           5    7    8    3
Old Subject        1    2    5    9

The main question is, if it is possible to read all the data in line 1 for instance and assign it to proper fields, like SubjName = Subject1, M1 = 5, M2 = 7, M3 = 8 and so on, WITHOUT using substrings? (something like stream >> Subject.SubjName; stream >> Subject.M1 = 5 and so on in C++).

Here's the code that I have.

internal void Read()
{
        TextReader tr = new StreamReader("Data.txt");
        string line;
        while ((line = tr.ReadLine()) != null)    //read till end of line
        {
            tr.ReadLine();    //Skips the first line


        }

Thanks in advance

EDIT: To clarify, I'd prefer that fields are delimited.

svick
  • 236,525
  • 50
  • 385
  • 514
zmockus
  • 329
  • 1
  • 5
  • 12
  • 1
    How is the data stored in the file? Any field delimiters? Or are the fields fixed length? – Oded Feb 04 '12 at 13:56
  • As for your edit - since you say you are the one writing to the file, how you delimit the fields is entirely up to you. – Oded Feb 04 '12 at 14:54

3 Answers3

2

Something like the solution in this question might help, but obviously use a tab delimeter (\t)

CSV to object model mapping

 from line in File.ReadAllLines(fileName).Skip(1)
    let columns = line.Split(',')
    select new
    {
      Plant = columns[0],
      Material = int.Parse(columns[1]),
      Density = float.Parse(columns[2]),
      StorageLocation = int.Parse(columns[3])
    }
Community
  • 1
  • 1
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
1

It is not clear from your question how the records are stored in the file - whether fields are delimited or fixed length.

Regardless - you can use the TextFieldParser class, which:

Provides methods and properties for parsing structured text files.

It lives in the Microsoft.VisualBasic.FileIO namespace in the Microsoft.VisualBasic.dll assembly.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

Split and Dictionary and your two methods of choice here. You read in your line, split it by empty spaces, then save it as a name/object pair in a dictionary.

Put the code below into a *.cs file, then build and run it as a demo:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace stringsAsObjects
{

    class stringObject
    {
        public static int Main(string[] args)
        {
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("Data.txt");
            string nameLine = file.ReadLine();
            string valueLine = file.ReadLine();


            file.Close();

            string[] varNames = nameLine.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            string[] varValues = valueLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary<string, object> map = new Dictionary<string, object>();

            for(int i = 0; i<varNames.Length; i++)
            {
                try
                {
                    map[varNames[i]] = varValues[i];
                }
                catch (Exception ex)
                {
                    map[varNames[i]] = null;
                }
            }

            foreach (object de in map)
            {
                System.Console.WriteLine(de);
            }

            Console.ReadKey();
            return 0;

        }

    }
}
Fuzzy Analysis
  • 3,168
  • 2
  • 42
  • 66