Click here to Skip to main content
15,898,708 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to read the following file and store into an array. First I need to split the strings separated by space and ; Further more i need to plot it. Can some one help me please?
150101 00:21:49,7 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 00:21:49,8 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 00:21:49,9 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:49,0 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,1 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,2 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,3 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,4 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,5 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,6 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,7 0030;0000;00;00;00;00;0000;0000;00;10;E008
150101 00:21:50,8 0030;0000;00;00;00;00;0000;0000;00;10;E008


What I have tried:

I have tried to read the file and store it in an array
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ComPlay
{
    class Read
    {
        
            private string[] header;
            private float[,] data;
            private int nLines;
            private int nColumns;

            //constructor to be implemented
            public Read()
            {
            }

            //functions used for retrieving the data
            public int get_nLines()
            {
                return nLines;
            }

            public int get_nColumns()
            {
                return nColumns;
            }

            public float[,] get_Data()
            {
                return data;
            }

            public string[] get_Header()
            {
                return header;
            }
        public Read(Stream myStream)
        {
            string aux;
            string[] pieces;   

            //read the file line by line
            StreamReader sr = new StreamReader(myStream);
            aux = sr.ReadLine();                       
            header = aux.Split(' ');
            nColumns = header.Length;    
            nLines = 0;
            while ((aux = sr.ReadLine()) != null)
            {
                if (aux.Length > 0) nLines++;
            }

            //read the numerical data from file in an array
            data = new float[nLines, nColumns];
            sr.BaseStream.Seek(0, 0);
            sr.ReadLine();
            for (int i = 0; i < nLines; i++)
            {
                aux = sr.ReadLine();
                if(i<2)
                  { pieces = aux.Split(' '); }

                else
                { pieces = aux.Split(';');}
                for (int j = 0; j < nColumns; j++)

                    data[i, j] = float.Parse(pieces[j]);
            }
            sr.Close();
        }
    }
}
Posted
Updated 3-Mar-16 0:27am
v2
Comments
Sascha Lefèvre 2-Mar-16 10:01am    
I'd suggest using a proven solution for reading CSV:
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
Member 12321270 6-Mar-16 17:06pm    
Thanks alot :) Sascha :)

Have you tried regular expressions?
It´s very useful to know how they work (painful experience).

C#
var table = new List<string[]>();
using (var r = new StreamReader("filePathOrStreamHere"))
{
  while (!r.EndOfStream)
  {
    string line = r.ReadLine();
    table.Add(Regex.Split(line, @"\s|[;]|[,]"));
  }

  r.Close();
}
//table now contains a list of string arrays,
// where each array is a "row" of "cell" values.


When you are using the data later you can convert it into the datatype based on index?
 
Share this answer
 
Comments
Member 12321270 3-Mar-16 6:39am    
Thanks alot all of you for such nice comments. I really appreciate that :)
Actually I can split the rows separated by empty spaces and semicolons. Also i can store them in arrays. But now I want to convert the string arrays to integer so that I can plot a graph from integer data.. Kindly if someone can provide me with some solution. My code looks like this:
//read the file line by line
StreamReader sr = new StreamReader(myStream);
aux = sr.ReadLine();


string[] separators = { @" " };
string[] words = aux.Split(separators, StringSplitOptions.RemoveEmptyEntries);

string[] separators1 = { @";" };
string[] words2 = words[2].Split(separators1, StringSplitOptions.RemoveEmptyEntries);

nColumns = words.Length;
nLines = 0;

while ((aux = sr.ReadLine()) != null)
{
if (aux.Length > 0) nLines++;
}

//read the numerical data from file in an array
data = new int[nLines, nColumns];
sr.BaseStream.Seek(0, 0);
sr.ReadLine();

for (int i = 0; i < nLines; i++)
{
aux = sr.ReadLine();
string[] words3 = aux.Split(separators, StringSplitOptions.RemoveEmptyEntries);

string[] words4 = words3[2].Split(separators1,StringSplitOptions.RemoveEmptyEntries);



}
sr.Close();
Per Söderlund 5-Mar-16 16:51pm    
What´s wrong with the solution i just gave you?
Rows are already splitted from newlines.
Try my code and then do 2 loops like this.

foreach(var row in table){
foreach(string cell in row){
//Convert cell into whatever datatype you are looking for.
}
}
Member 12321270 6-Mar-16 17:02pm    
Thank you very much :) Per Söderlund.
I tried and its great :)
Per Söderlund 7-Mar-16 4:54am    
Np
Why to force doors wide open?

Use Ado.net[^]. It is a set of classes that expose data access services to the .NET programmer.
See:
ADO.NET (core data access)[^]
Read Text File (txt, csv, log, tab, fixed length)[^]
Much ADO About Text Files[^]
Read Text File Specific Columns[^]
 
Share this answer
 
Comments
Member 12321270 6-Mar-16 17:05pm    
Thanks alot :) Maciej Los
Maciej Los 6-Mar-16 17:07pm    
You're very welcome
As Sascha says, a pre-built CSV reader is a much better idea - and the one he suggests is the one I use as well: A Fast CSV Reader[^]
For charting, it's pretty easy once you've read the data: Using a Chart With Multiple Lines From A Collection[^] should help.
 
Share this answer
 
Comments
Member 12321270 6-Mar-16 17:05pm    
Thank you very much :)
OriginalGriff 6-Mar-16 17:18pm    
You're welcome!
Member 12321270 11-Mar-16 7:20am    
I have read your article "Using a Chart With Multiple Lines From A Collection".But I could not understand the code and how it works.
OriginalGriff 11-Mar-16 7:30am    
What part didn't you understand?
It's pretty simple, and includes a demo download...
Member 12321270 11-Mar-16 8:15am    
My read class reads my file, split the strings, convert them to integer except second and last columns. After that I dont know how to proceeds to plot the integer data.. My read class looks like:

class Read
{

// private string[] header;
private int[,] data;
private int nLines;
private int nColumns;

//constructor to be implemented
public Read()
{
}

//functions used for retrieving the data
public int get_nLines()
{
return nLines;
}

public int get_nColumns()
{
return nColumns;
}

public int[,] get_Data()
{
return data;
}


public Read(Stream myStream)
{


var table = new List<string[]>();
var tableint = new List<int[]>();
using (var r = new StreamReader(myStream))
{
while (!r.EndOfStream)
{
string line = r.ReadLine();
table.Add(Regex.Split(line, @"\s|[;]"));

}

r.Close();
}
//table now contains a list of string arrays,
// where each array is a "row" of "cell" values.
int[] arr = new int[13];

foreach (var row in table)
{

int a = 0;
foreach (string cell in row)
{
//Convert cell into whatever datatype you are looking for.

//tableint.Add(Convert.ToInt32(cell));
// int value = Convert.ToInt32(cell);

if (a>1 && a<12)
{
arr[a] = Convert.ToInt32(cell);

}
a++;


}
tableint.Add(arr);
}

}

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