Sunday, July 26, 2009

How to display and read .txt file using C# windows application?

Help! I'm kinda confused on doing this program.





You have a .txt file (from Notepad) that has the following input in the first line:





1234567#John Edwards#Ohio





How can you able to view and display these outputs using the C# application, in such a way that '1234567','John Edwards' and 'Ohio' are different inputs, wherein, they are all displayed separately in a label/text button?





Is there any links I could access that involves this kind of problem? Thanks.

How to display and read .txt file using C# windows application?
I don't know C#.





But in C, I will do this like





{


FILE *fp;


fp = fopen("..........txt","r");


fscanf(fp,"%ul#%s#%s",%26amp;code,name,state...


}





The # symbol in scanf swallows the matching character in the text file. (However, the name 'John Edwards' has two parts. Only 'John' will be stored in name)





I did C long back in 1996. And I haven't practically did any project.





I just tried to help you recollecting my old days.
Reply://FilePath IS THE PATH OF THE FILE YOU NEED TO READ


StreamReader obj_StreamReader = new StreamReader(FilePath);


string str = null;





//LIST THAT SAVE THREE DIFFERENT INPUTS


ArrayList obj_ArrayList = new ArrayList();





while ((str = obj_StreamReader.ReadLine()) != null)


{


string[] strr = str.Split('#');//SPLIT THE STRING


foreach (string split_str in strr)


{


obj_ArrayList.Add(split_str);


}





//TAKE THREE LABEL CONTROLS AND CHANGE THERE NAME PROPERTY TO LBL1, LBL2 AND LBL3


//OR ANYOTHER NAME OF YOUR CHOICE. AND DO THE FOLLOWING


LBL1.Text = obj_ArrayList[0].ToString();//THIS DSPLAY 1234567


LBL2.Text = obj_ArrayList[1].ToString();//THIS DSPLAY John Edwards


LBL3.Text = obj_ArrayList[2].ToString();//THIS DSPLAY Ohio


}
Reply:check out any documentation on the System.IO name space. within the name space are several classes that deal with handling files. You will probably need to learn a little about StreamReader objects and how they allow you to read files. The File class also gives you some power in how to handle files.

violet

No comments:

Post a Comment