Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the content of my text file. First I have to read the text file and then I have to create the instances of every signal through another class. I am able to extract the data in a dictionary. Now I have to create another class that will make objects of Signal class.Could you guys please help me out.
Thanks.

Signal 1
00000000111101010

Signal 2
1010

Signal 3
0111100001110


What I have tried:

C#
  class Signals
    {

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private ulong data;

        public ulong Data
        {
            get { return data; }
            set { data = value; }
        }
        
    }
class Program
    {
        static void Main(string[] args)
        {
 StreamReader file = new StreamReader(@"C:\Users\chaudhry\source\repos\ReadingTextFile\ReadingTextFile\SignalFile.txt");
         
            List<string> datas = new List<string>();

            Dictionary<string, ulong> dict = new Dictionary<string, ulong>();
            string line = string.Empty;
             while ((line = file.ReadLine()) != null)

            
            {
                if (line != "")
                {
                    datas.Add(line);

                }

            }
            file.Close();

            for (int i = 0; i < datas.Count; i = i + 2)
            {

                dict.Add(datas[i], ulong.Parse(datas[i + 1]));

               

            }
foreach (var kvp in dict)
            {
                Signals signals = new Signals();
                signals.Name = kvp.Key;
                signals.Data = kvp.Value;

                Console.WriteLine(" Name : {0} Data :{1}",signals.Name,signals.Data);


                
                

            }

           




        }

        }     
Posted
Updated 9-Apr-18 23:32pm
v4

1 solution

I'd suggest to use File.ReadAllLines Method (System.IO)[^] to read data from text file.

[EDIT]
Your class needs some improvements:

According to our discussion, i've made some changes to the class:
C#
public class Signal
{
	private string sName = string.Empty;
	private byte[] oData = new byte[]{};
	
	public Signal(string _Name, string _data)
	{
		sName = _Name;
		oData = _data.Select(x=>byte.Parse(x.ToString())).ToArray();
	}
		
	public string Name
	{
		get => sName;
		set => sName = value;
	}
	
	public byte[] Data
	{
		get => oData;
		set => oData = value;
		
	}
	
	public List<KeyValuePair<byte, int>> Map
	{
		get
		{
			List<KeyValuePair<byte, int>> map = new List<KeyValuePair<byte, int>>();
			int i = 0;
			int j = 0;
			int l = oData.Length;
			while(i<l)
			{
				int k = 0;
				j = i;
				while(oData[i]==oData[j])
				{
					k++;
					j++;
					if(j==l) break;
				}
				KeyValuePair<byte, int> kvp = new KeyValuePair<byte, int>(oData[i], k);
				map.Add(kvp);
				i = j-1;
				i++;
			}
			return map;
		}
	}
	
	public override string ToString()
	{
		return string.Format("{0}\n{1}\n", sName, string.Join("", oData));
	}
}


Usage:
C#
void Main()
{
	string[] lines = File.ReadAllLines("FullFileName.txt");
	//get names
	var names = lines
		.Where(x=>x.Trim().StartsWith("S"))
		.ToList();
        //get data
	var datas = lines
		.Where(x=>!x.Trim().StartsWith("S") && x.Trim()!=string.Empty)
		.ToList();
        //create list of Singal class
	List<Signal> signals = names.Zip(datas,
		(n, d)=> new Signal(n, d))
		.ToList();
	foreach(Signal s in signals)
	{
		Console.WriteLine("{0} '{1}'=>[{2}]", s.Name, string.Join("", s.Data), string.Join("|", s.Map.Select(x=>x.Key + ":" + x.Value)));
	}

}


Sample output:
Signal 1 '00000000111101010'=>[0:8|1:4|0:1|1:1|0:1|1:1|0:1]
Signal 2 '1010'=>[1:1|0:1|1:1|0:1]
Signal 3 '0111100001110'=>[0:1|1:4|0:4|1:3|0:1]


Good luck!
 
Share this answer
 
v2
Comments
hamid18 9-Apr-18 3:35am    
Thanks a lot Maciej.
Maciej Los 9-Apr-18 3:46am    
You're very welcome.
hamid18 9-Apr-18 8:05am    
The data is repeated pattern of 0s and 1s. I do not want to save this repeated pattern of data. I want to make a map like the one which I have mentioned below. I know I cannot use dictionary here. As the key should be a unique one in the dictionary. I do not know any other data structure which does mapping. The way I want to do. Because in the end I will retrieve the original data from this mapping.
0 5
1 6
0 4
1 3
0 1
Maciej Los 9-Apr-18 9:07am    
Sorry, but i don't get you... What you mean by "The data is repeated pattern of 0s and 1s."?
Is that mean you've got only Signal 0 and 1? Do you want to group data by its name and sum ulong data?
[EDIT]
If i understand you well, you don't need to use Dictionary object. See:
	var signals = names.Zip(datas,
		(n, d)=> new Signal(n, d))
		.GroupBy(x=>x.Name)
		.Select(grp=> new Signal(grp.Key, (ulong)grp.Sum(y=>(decimal)y.Data)))
		.ToList();

Try!
hamid18 9-Apr-18 10:10am    
Actually the Data property of every instance of signal contains values like 01000011111100000111000101. Here we have repetition of same numbers. We want to compact it. like we can make a map that
0 1
1 1
0 4
1 6
0 5
this shows that if we start from the first digit we have one 0s. Then we have one 1s. Then we have four 0s. In this way we are able to save the entire sequence of numbers in int data type. And in the end we can get back the original data using same map.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900