Click here to Skip to main content
15,567,482 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi All,
I'm completely new to programming in C# and have this question of how to go about parsing a .txt file?
My .txt file looks something like this
Value	29.500000
ID      abcde
Matrix1
1.000000	0.000000	0.000000	0.000000	
0.000000	1.000000	0.000000	0.000000	
0.000000	0.000000	1.000000	0.000000	
0.000000	0.000000	0.000000	1.000000	
Point1 	0.000000	0.000000	0.000000
Point2	1.000000	0.000000	0.000000
Point3	0.000000	1.000000	0.000000
Point1Dim	-1.000000
Point2Dim	-1.000000
Size1	30.000000
Size2	20.000000
Size3	15.000000
:
:
:

and so on. And my goal is to extract Point1, Point2 and so on.
Help and guidance would be greatly appreciated.
Thanks
Rinkie

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 30-Oct-13 8:23am
v2
Comments
Richard C Bishop 30-Oct-13 13:18pm    
First search, "how to read a .txt file". Then apply that code to your project. You can then filter out the data in a datatable after it is filled with the lines of your text file.
Sergey Alexandrovich Kryukov 30-Oct-13 14:23pm    
Are you sure you really want your own parser, not something like XML or JSON, where you can use one of available parsers readily available from .NET FCL?
—SA

The first thing you will need is a StreamReader[^] to read the file, either one line at a time with ReadLine[^] or all in one go with ReadToEnd[^]. You can then use methods of the String[^] class to find specific lines and split the text into separate fields. Finally use the Double.Parse[^] method to convert character strings to Double values.
 
Share this answer
 
There are no real short cuts when it comes to stuff like this.
Firstly, it seems to be organised in lines: so start by converting it to a collection of lines.
C#
string[] lines = File.ReadAllLines(path);

Then, I would probably set up a List<string>for each type you want to extract:
C#
List<string> Points = new List<string>();
List<string> PointDim = new List<string>();
List<string> Size = new List<string>();
...</string></string></string></string></string></string>
Then I'd start looping through the lines separating out the lines into the appropriate collections.
Finally, Id start looking at the collections, and converting them into internal classes (or collections of internal class instances)
 
Share this answer
 
C#
IEnumerable<string> allLines; 
string filePath = @"C:\data\data.txt";
 if (File.Exists(filePath))
 {
  //Read all content of the files and store it to the list split with new line 
   allLines = File.ReadLines(filePath);
 }

 //all Point 1 lines
 IEnumerable<string> point1Lines = allLines.Where(d => d.StartsWith("Point1", StringComparison.CurrentCultureIgnoreCase));

foreach(string line in point1Lines)
{
  string[] values= line.Split(' ');//either space or tab or others as your file contain seperator  
  for(int i=1; i <words;>  {
      string value = values[i];// skip index 0,it contains label, remaining are point data
     //logic goes here
  }
}</string></string>
 
Share this answer
 
v2

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