Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to read a text file with unknown number of fields and unknown type of delimiters into a structure and do it in a class?
Examples of files:
The text file has an unknown name and could have from one to unlimited fields.
1. tom,bicycle
2. "Car""building""lady"
3. desk <tab> chair <tab> man <tab> eat
4. for watt John water noway
5. The-Yellow-Brick-road-is-long

I can create a structure but I know the number of fields and the delimiter. Can this be put into a class?
Posted
Updated 29-Sep-11 5:18am
v2

1 solution

It is possible to create new properties at runtime and therefor create a specialized class for each list of fields if it was not yet created.
http://msdn.microsoft.com/en-us/library/h1zby21a.aspx[^]

There could be a problem that might make it somewhat useless. What I mean is that you create a class definition at runtime with properties that weren't known at compile time. This means that you actually make it quite hard on yourself to use it.

A better solution would probably be a generic class with an index property for reading each field by index and a count property to get the number of fields available. This class is used by a class that reads the file and also has an indexed property for reading each line by index (which returns an object of the previous described generic class) and also has a count property to get the number of lines. See the link for more info about an indexed properties.

http://www.vb-helper.com/howto_net_indexed_property.html[^]


Good luck!
 
Share this answer
 

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