To parse a fixed-width text file:

using (FixedWidthFieldParser parser = new FixedWidthFieldParser("error.log"))
{
	parser.SetFieldWidths(3, 20, -1);
	
	while (!parser.EndOfFile)
	{
		try
		{
			// Reads the next record
			TextFields fields = parser.ReadFields();
			
			// Gets parsed data
			error.Code = fields.GetInt32(0);
			error.Message = fields.GetString(1);
			error.source = fields.GetString2);
			...
		}
		catch (MalformedLineException e)
		{
			// Handle exception here.
		}
	}
}

Last edited Jul 7, 2009 at 6:41 PM by mtricoli, version 3

Comments

No comments yet.