Click here to Skip to main content
16,008,469 members
Home / Discussions / C#
   

C#

 
QuestionParse a text file into an object Pin
AndyHug28-Aug-06 8:41
AndyHug28-Aug-06 8:41 
AnswerRe: Parse a text file into an object Pin
Ed.Poore28-Aug-06 9:14
Ed.Poore28-Aug-06 9:14 
AnswerRe: Parse a text file into an object Pin
Not Active28-Aug-06 9:15
mentorNot Active28-Aug-06 9:15 
AnswerRe: Parse a text file into an object Pin
SomeGuyThatIsMe28-Aug-06 9:16
SomeGuyThatIsMe28-Aug-06 9:16 
GeneralRe: Parse a text file into an object Pin
Guffa28-Aug-06 9:35
Guffa28-Aug-06 9:35 
GeneralRe: Parse a text file into an object Pin
SomeGuyThatIsMe28-Aug-06 9:42
SomeGuyThatIsMe28-Aug-06 9:42 
GeneralRe: Parse a text file into an object Pin
AndyHug28-Aug-06 9:41
AndyHug28-Aug-06 9:41 
GeneralRe: Parse a text file into an object Pin
SomeGuyThatIsMe28-Aug-06 10:09
SomeGuyThatIsMe28-Aug-06 10:09 
ick, i hate VB, mostly because the vast majority of examples you find for it never bother with strict typing, which if you're going to translate to c# is very necessary.

ok i'm going to assume that all this needs to do is read the file, then write that same data to a different file, i havnt messed with generics, but i would either make the "superclass" have properties of the other types, or make the other types like header or comment inherit the superclass, but i havnt messed w/ generics so there's prolly some slick, cool way to do this that i'm not aware of.

but to read the file in c# (you get to declare the varibles cuz i'm too lazy to type that), i'm also going to assume that the super class has properties of the type of the other classes so i can get to them like record.header.*something a header has or does*

while(!endOfFile){//endOfFile is a bool initialized to false
line = reader.ReadLine();//line is a string, guess what reader is
firstChar = line.SubString(0,1);//pretty easy so far right?
switch(firstChar){
Case 'H':
record.Header.value = line;/*record is an instance of the superclass, value is the property you want the header stored in, if you want the first letter gone you'll have to remove it with a line = line.Remove statement*/
break;
Case 'I':
//do whatever you need to do to save the information in the record object
break;
/*keep going until you've added everything for a certian record..if you can have mulitple comments the comments property of the record class may need to be an array or you'll have to figure out some other way to handle it*/
}//end switch
recordList.Add(record);/*adds the record object to an ArrayList of record objects..or you could use a normal array if you know how many records you'll have*/

}//end while dotn forget on case 'E' you have to set end of file to true so the loop ends


to write the file you'll just need a StreamWriter then just use a foreach loop to get your stuff back

foreach(Record r in RecordList){

writer.write(r.header.value.ToString());//so on and so forth, you'll have to so something speical for the comment part if you can have more than one...if you used an array or array list you can just loop through the comments like you are the records in record list

}//end foreach

and tada you're done...i think, i'm not gaurenting that this code will work( i wrote it on this site not in visual stuio and i havnt tried it, but the methodology does work i've used that several times before. if you need anymore help or have any questions shoot me an email to dalaji@excite.com(its my spam account so put CodeProject help or something like that in the subject so i dont delete you)
GeneralRe: Parse a text file into an object Pin
AndyHug30-Aug-06 4:31
AndyHug30-Aug-06 4:31 
GeneralRe: Parse a text file into an object Pin
SomeGuyThatIsMe30-Aug-06 5:02
SomeGuyThatIsMe30-Aug-06 5:02 
QuestionProgress Bar: File Copy Pin
numbers1thru928-Aug-06 8:13
numbers1thru928-Aug-06 8:13 
AnswerRe: Progress Bar: File Copy Pin
pasha2k28-Aug-06 8:31
pasha2k28-Aug-06 8:31 
AnswerRe: Progress Bar: File Copy Pin
mav.northwind28-Aug-06 8:32
mav.northwind28-Aug-06 8:32 
GeneralRe: Progress Bar: File Copy Pin
pasha2k28-Aug-06 8:57
pasha2k28-Aug-06 8:57 
GeneralRe: Progress Bar: File Copy Pin
mav.northwind28-Aug-06 9:30
mav.northwind28-Aug-06 9:30 
AnswerRe: Progress Bar: File Copy Pin
Nader Elshehabi28-Aug-06 8:35
Nader Elshehabi28-Aug-06 8:35 
GeneralRe: Progress Bar: File Copy Pin
pasha2k28-Aug-06 8:51
pasha2k28-Aug-06 8:51 
GeneralRe: Progress Bar: File Copy Pin
Nader Elshehabi28-Aug-06 9:54
Nader Elshehabi28-Aug-06 9:54 
GeneralRe: Progress Bar: File Copy Pin
numbers1thru928-Aug-06 9:47
numbers1thru928-Aug-06 9:47 
QuestionDatagridViewCaption? Pin
Itanium28-Aug-06 7:07
Itanium28-Aug-06 7:07 
AnswerRe: DatagridViewCaption? Pin
Nader Elshehabi28-Aug-06 7:43
Nader Elshehabi28-Aug-06 7:43 
QuestionHttpWebRequest from c# app Pin
SomeGuyThatIsMe28-Aug-06 6:41
SomeGuyThatIsMe28-Aug-06 6:41 
QuestionKeycode Error on Crystal Report Pin
paulcortez28-Aug-06 6:29
paulcortez28-Aug-06 6:29 
QuestionDataViewManager Pin
~~~Johnny~~~28-Aug-06 6:17
~~~Johnny~~~28-Aug-06 6:17 
AnswerRe: DataViewManager Pin
Nader Elshehabi28-Aug-06 7:34
Nader Elshehabi28-Aug-06 7:34 

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.