Click here to Skip to main content
15,879,613 members
Articles / Programming Languages / C#

Text Parser for C#

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
26 Oct 20061 min read 83.6K   699   37   13
This class parses text data stored in String or TextReader objects, i.e., it is suitable for loading of text files.

Introduction

While .NET class BinaryReader provides many useful methods, e.g., ReadByte, ReadDouble, etc., there are no such methods in TextReader. As far as I know, there is neither a .NET class similar to good Java Scanner class (java.util.Scanner) nor a .NET class providing methods similar to C/C++ CRT function scanf. This means that the reading of text files may become, especially for beginners, a nightmare. The common way is to read the whole line, split it into tokens and convert these tokens into the required format (Byte, Double, Int32, ...) but it is not very convenient. Therefore I decided to program my own class to simplify text data processing.

Using the Code

ZCU.IO.TextParser is a small class written in C# 2.0 that is able to read text data from String or TextReader object. It provides methods for reading numbers, words, tokens matching the given pattern, etc. It also contains the method Scanf for formatted reading.

The following example, which can read 2D or 3D points from the text file, demonstrates the use of this class.

Input file 1

100
0.10330 0.00540
0.42320 0.49370
0.23690 0.56980

etc.

Input file 2

100
0.10330 0.00540 0.23690
0.42320 0.49370 0.23690
0.23690 0.56980 0.10330

etc.

The code for loading input files is as follows:

C#
StreamReader lReader = new StreamReader(new FileStream(strFilePath, 
		FileMode.Open, FileAccess.Read, FileShare.Read),Encoding.UTF8);

ZCU.IO.TextParser parser = new ZCU.IO.TextParser(lReader);

//loads the number of points in the file
uint nNumVert = parser.ReadUInt32();
m_TriData.Vertices = new TRS_VERTEX[nNumVert];

//reads the first point
string line = parser.ReadLine();
ZCU.IO.TextParser line_parser = new ZCU.IO.TextParser(line);

//buffer for objects retrieved by Scanf
object[] xyz = new object[3];
xyz[2] = new System.Single();        //0,1(x,y) are always created by Scanf
string format = "%f %f %f\n";
if (line_parser.Scanf(format, xyz) < 3)
    format = "%f %f\n";              //z-coordinate not in file

List<STRING> formatList = ZCU.IO.TextParser.ConstructFormatList(format);

m_TriData.Vertices[0].x = (float)xyz[0];
m_TriData.Vertices[0].y = (float)xyz[1];
m_TriData.Vertices[0].z = (float)xyz[2];

for(uint i = 1; i < nNumVert; i++)
{
    parser.Scanf(formatList, xyz);
    m_TriData.Vertices[i].x = (float)xyz[0];
    m_TriData.Vertices[i].y = (float)xyz[1];
    m_TriData.Vertices[i].y = (float)xyz[2];
}

Points of Interest

The source code can be used, modified and redistributed under the terms of the license agreement that is included in the deployment package.

History

  • 26.10.2006 - version 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReading lrc file Pin
Megalan27-Feb-09 3:30
Megalan27-Feb-09 3:30 
GeneralLp,BP,HP filters design in C# [modified] Pin
Keleistein9-Apr-07 21:57
Keleistein9-Apr-07 21:57 
GeneralRe: Lp,BP,HP filters design in C# Pin
Keleistein12-Apr-07 14:29
Keleistein12-Apr-07 14:29 
GeneralReading text file Pin
kelvinic3-Apr-07 18:19
kelvinic3-Apr-07 18:19 
GeneralRe: Reading text file Pin
BeSoft3-Apr-07 21:17
BeSoft3-Apr-07 21:17 
GeneralRe: Reading text file Pin
kelvinic3-Apr-07 22:08
kelvinic3-Apr-07 22:08 
GeneralRe: Reading text file Pin
BeSoft3-Apr-07 22:56
BeSoft3-Apr-07 22:56 
GeneralRe: Reading text file Pin
kelvinic3-Apr-07 23:00
kelvinic3-Apr-07 23:00 
GeneralRe: Reading text file Pin
BeSoft3-Apr-07 23:29
BeSoft3-Apr-07 23:29 
GeneralRe: Reading text file Pin
kelvinic4-Apr-07 1:55
kelvinic4-Apr-07 1:55 
GeneralRe: Reading text file Pin
BeSoft4-Apr-07 2:36
BeSoft4-Apr-07 2:36 
GeneralRe: Reading text file Pin
Keleistein9-Apr-07 21:59
Keleistein9-Apr-07 21:59 
QuestionSample Question Pin
sides_dale1-Nov-06 18:09
sides_dale1-Nov-06 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.