C# Extract datas from a text file

Hello, here’s my programme.
(I’m french but I try to write english).
I explain what I want to do in comments, but I summarize the goal :
Here an extract of one of my text files :

Suj01_PI_DP_C00_1
Date:,2014/11/06
Time:,14:55:12
Type:,test
Description:,“”
Notes:,“”

TRAJECTORIES
100.000000,Hz
,Suj01:RIAS,Suj01:LIAS,Suj01:LIPS,Suj01:RIPS,Suj01:DSTR,Suj01:C7,Suj01:RACR,
Field #,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X
1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,

I want to extract datas from a text file to a matrix in Unity. For that, I create a matrix and I integrate all datas in it. Then, I want to load (read) the file but only from the 12th line. And I have to extract only 1 line / 4 of datas.

I want the matrix doesn’t load “1,” which indicates the line’s number of datas. And it’s wrote at every line : “2,”, “3,” , …

Then, I want to integrate the name of items “Suj01:RIAS”, “Suj01:LIAS”, … (There are more than 55 names like them) in a string. The same thing for the positions “X”, “Y”, “Z”.
Every group of three datas (separated by a comma) corresponds to a name in the order. The firt group corresponds to “Suj01:RIAS” for example. And every data in a group, corresponds to a position according to X, Y and Z, in the order. The first data corresponds to X, the second data to Y and the third to Z. For every group of 3 datas.

Could you see my programme and tell me where there is a problem ?

Here my programme :

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

public class Recup_donnees_3 : MonoBehaviour {

// When the script instance is being loaded
void Awake ()	{
	
	Application.targetFrameRate = 25; // J'indique que la fréquence est de 25 Hz
}

class FileReader
	
{
	
	static void Main()
		
	{

// ------------- This first part allows me to read in the console and to load in a matrix 1 line / 4 from the 12th line of the text file to the end ------------- //

					// Create an instance of StreamReader to read from a file
		
					StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");
		
		
					using (reader) { // Automatic Closing of the Stream after working with it , otherwise we would have to write "reader.Close" at the end to close the stream, beneath the last command's line of this part "class FileReader"

					int lineNumber = 12;
		
		
		
					// Read 12th line from the text file
		
					string line = reader.ReadLine ();
		
		
		
					// Read the other lines from the text file : 1 line / 4
		
					while (line != null) {
			
					lineNumber = lineNumber + 4;
			
					// Console.WriteLine("Line {0}: {1}", lineNumber, line); // To show in the console 
			
					line = reader.ReadLine ();

					string[] Matrice_Suj01_PI_DP_C00_1; // To create a matrix which contains 1 line / 4 from text file

					Matrice_Suj01_PI_DP_C00_1 = reader.ReadLine (); // To indicate what datas will be loaded in this matrix named "Matrice_Suj01_PI_DP_C00_1"

							}

// ------------- This second part allows me to extract some datas from the text file ------------- //

					// To separate every group of 3 datas corresponding to a one "marqueur"

					string[] values_position_X_Y_Z = Matrice_Suj01_PI_DP_C00_1.Split(',',3); // Pb : we can't use "Split" when it's a string []

					// To extract the axes positions (X Y Z) and to integrate them in a string

					int line_axes = 11;
			
					line_axes = reader.ReadLine ();
			
					string line_11;
			
					line_11 = reader.ReadLine ();
			
					string[] axes = line_11.Split(new char[] {'Filed #',','}); // Every axe is separating by ',' and the first is writing behind 'Filed #,'

					// To extract the names of "marqueurs" and to integrate them in a string

					int line_names = 10;

					line_names = reader.ReadLine ();

					string line_10;

					line_10 = reader.ReadLine ();

					string[] names = line_10.Split(new char[] {',',',,,'}); // Every name is separating by ',,,' and the first is writing behind ','

// ------------- This third part allows me to indicate column’s name “Positionnements des marqueurs : RIAS, LIAS, …” for every group of 3 positions (every group X,Y,Z)------------- //

					// To indicate the correspondance between these strings : "values_position_X_Y_Z" and "names"

					string[] names = String.Join("/", values_position_X_Y_Z, 2, 3); // From the second data after "," from the 12th line, I associate 3 datas with the 10th line
					}
			}
}

}

I didn’t really understood completely the question but here is a snippet that will let you read line number 12 from a file and a snippet that let you read a line every 4 lines from line 12.

To use this snippets remember to add to your class file

using System.Linq;  
using System.IO;

To read just line 12

//Read just line 12
IEnumerable<string> fileLines = File.ReadLines("Test.txt");

string line12 = fileLines.Skip(11).Take(1).First();

To read from line 12 and each line after 4 lines

//Read from line 12 and each line after 4 lines (e.g.: 16, 20, 24, ...)
string[] lines;

lines = File.ReadAllLines("Test.txt");

//In C# an array has a 0-based index
int lineIndex = 11;

for(int i = lineIndex; i < lines.Length; i+= 4)
{
    DoSomethingWithLine(lines*);*

}
Keep in mind that the second method with File.ReadAllLines(string) is best if the file you want to read is small.
If the file is very big you can StreamReader.ReadLine until you reach the desired one and discard the others, more or less like so:
using (StreamReader stream = new StreamReader(File.OpenRead(“Test.txt”)))
{
//We are starting from 0 so me need to subtract 1
int lineSkip = 11;

while(!stream.EndOfStream)
{
for(int i = 0; i < lineSkip && (!stream.EndOfStream); i++)
{
//Read the line and throw it away
stream.ReadLine();
}

if (!stream.EndOfStream)
{
DoSomethingWithLine(stream.ReadLine());
}
//As always we are starting from 0, so we need to subtract 1
lineSkip = 3;
}
}
I understand this last snippet is not the best snippet ever, but it should do its work.
If you just need to read the file from a starting point and the rest of it you can apply the same techniques like so:
Here by using File.ReadAllLines(string)
string[] lines;
lines = File.ReadAllLines(“Test.txt”);
//In C# an array has a 0-based index
int lineIndex = 11;
for (int i = lineIndex; i < lines.Length; i++)
{
DoSomethingWithLine(lines*);*
}
Here by read a line after another and discarding the ones you do not want
using (StreamReader stream = new StreamReader(File.OpenRead(“Test.txt”)))
{
//Skip 11 lines
for (int i = 0; i < 11 && (!stream.EndOfStream); i++)
{
stream.ReadLine();
}
while (!stream.EndOfStream)
{
DoSomethingWithLine(stream.ReadLine());
}
}
Let me know ! :slight_smile:

Actually, I have different text files. The lines which interested me are :

- Line n°10 :
,Suj01:RIAS,Suj01:LIAS,Suj01:LIPS,Suj01:RIPS,Suj01:DSTR,Suj01:C7,Suj01:RACR,Suj01:LACR,Suj01:USTR,Suj01:T4,Suj01:T8,
Suj01:T12,Suj01:L5,Suj01:GLAB,Suj01:HETP,Suj01:RTRA,Suj01:LTRA,Suj01:RELE,Suj01:RELI,Suj01:RELT,Suj01:RWRI,
Suj01:RWRE,Suj01:RWRT,Suj01:RHDE,Suj01:RTHU,Suj01:RHDI,Suj01:LELE,Suj01:LELI,Suj01:LELT,Suj01:LWRI,Suj01:LWRE,Suj01:LWRT,Suj01:LHDE,
Suj01:LTHU,Suj01:LHDI,Suj01:RKNE,Suj01:RKNI,Suj01:RKNT,Suj01:RANE,Suj01:RANI,Suj01:RANT,Suj01:RFOF,Suj01:RFOE,
Suj01:RFOI,Suj01:RFOB,Suj01:LKNE,Suj01:LKNI,Suj01:LKNT,Suj01:LANE,Suj01:LANI,Suj01:LANT,Suj01:LFOF,Suj01:LFOI,Suj01:LFOE,Suj01:LOFB,
Chariot:CHA4,Chariot:CHA2,Chariot:CHA1,Chariot:CHA3,Chariot:CAP1,Chariot:CAP2,Chariot:CAP3,Chariot:CAP4

Where I want only names like “Suj01:RIAS” “Suj01:LIAS” …

- Line n° 11 :
Field#,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,
X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,
X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z,X,Y,Z

Where I want positions axes. Every group “X,Y,Z” corresponds to a name like “Suj01:RIAS” …

- From the line n°12 to the end of the text file : (and 1 line / 4) (It’s a sample of 3 datas lines)
1,-242.807816,1106.551270,1097.119385,14.437944,1075.778687,1095.583008,-44.466446,1267.733887,1105.356445,-128.627472,1277.957642,1108.846191,
-121.277077,1059.626953,1370.202759,-103.197083,1198.642578,1640.340088,-299.121094,1233.381592,1589.360596,881.018532,12357.533691,1034.538818,1341.618652,
NaN,NaN,NaN,152.531693,1191.905396,1236.288818,82.196426,1208.939941,1225.688843,138.768158,1171.405396,1335.364502,187.567688,1088. …
2,-242.708282,1106.545654,1097.141479,14.532575,1075.795776,1095.581421,-44.284542,1267.713135,1105.405518,-128.476517,1277.957886,1108.822021,-121.214470, …
1059.845337,1370.132202,-103.143044,1198.702148,1640.381714,-299.018127,1233.573486,1589.335449,82.020554,1135.265381,1605.206299,-131.176544,1086.614258,1540.604126,
-89.283218,1250.712646,1548.286255,-80.893433
3,-242.599960,1106. …

I don’t copy all datas because there are lots of lines … And I don’t want to take in the array the number of lines, indicates at the begin of every datas line. “1,” “2,” “3,” …
And every data is a position axe. The first : X, the second : Y and the third : Z. For every group (of three datas). And e group corresponds to a name like “Suj01:RIAS” …

So I have to list. For example :

           Suj01:RIAS                          
  X            Y             Z               

-242.807816____1106.551270_____1097.119385

          Suj01:LIAS
X            Y                  ...

14.437944____1075.778687 …

For every line / 4