Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse a text file into an object

Status
Not open for further replies.

andyhug

Programmer
Aug 28, 2006
1
RO
hi,

I have an text file grouped into records that I need to be parsed into a object. The record is like :

H10000 ( H from header of the record )
I1,name,age,sex (Information1)
C1,text... (Comment 1 for Information1)
....
Cn,text... (Comment n for Information1)
I2,name,age,sex (Information2)
C1,text... (Comment1 for Information2)
....
Cn,text... (Commentn for Information2)
...
In,name,age,sex (Information n)
C1,text... (Comment1 for Information n)
....
Cn,text... (Commentn for Information n)

E1 (End message)

There is a class for each Tag (H,I,C,E) and a Record class that is the superclass for the classes(H,I,C,E) . I need to parse this file and store the informations from the records into a object using generics. For the informations I must use a list. I also need the reverse operation : from object to write to file.

I realize that the code must be written by myself but I would appreciate some general help about how to create the object from file

thanks

 
well the easiest way would be to use the filestream class and the split function

you say you want to do it urself so i dunn how much to say

but ass using System.IO; to the project

FileStream files = new File.OpenText(@"C:\test.txt");

now you can play with files

string[] inLine;

inLine = files.ReadLine().split(',');

now everything that split by , is stored in the array
so

name age sex
ernie,20,male

would be
inLine[0]=ernie
inLine[1]=20
inLine[2]=male

and so on

you can store it in a listview from there for example
or you can extend the array

Hope this gets you on the way

Cybrax

p.s. he filestream also allows yyou to write so you can (if you used it) read the listview and write back the data
 
By your example,
- Record starts at 'Hxxxx', ends with 'Exx'
- A Record has collections of nformation
- Each information has collections of [C]omment
- Information is a data structure w/ 3 attributes: name, age, sex, and a collection of comments.

So basically, your Record class can have 3 attributes: begin-tag ID, end-tag ID, collection of Information objects. The Record class can take in a string containing the entire record string so it can parse it, or the stream ref so that it can parse each line while moving the file pointer. It's up to your design / style.

Persisting the Record's data back to the file shouldn't be that difficult. You can just a have method on the Record class that takes in a ref to file stream and perform custom writing routine.

Based on the above suggestions, at a minimum, you should have 2 classes: Information class and Record class. There is no need for Comment class because it's basically a string, unless if you need to persist other data like a comment ID.


my 2 cents. [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top