开发者

collect text file information using c#

I have a text file named abc.txt. The file have info like this

jani
stay in USA
karim
stay in France
fara
stay in UK

The code needs to read the file from beginning and read the first line and put it to in a string let nameString. Then it re开发者_运维百科ad the second line and put the info to a another string let name addressString. Then it read the third line and put the info into nameString and then forth line and put the info as addressString. Like this way it will continue up to the end of file. I have to put those information on anywhere else. So, I need all the name and address in a for loop.

Can anybody pls help me to collect this info.

Thanks in advance

Riad


Here we go

 StreamReader SR;
 string S;
 SR = File.OpenText(filename);
 S = SR.ReadLine();
 string name = "";
 string address = "";
 counter = 1;
 while (S != null)
 {
    if ((counter % 2) != 0)
       name = S;
    else
       address = S;

    //do what you want with name and address here

    S = SR.ReadLine();
    counter++;

 }
 SR.Close();

Make sense?


You need to figure out how to read from text files in c# Here is a resource for you:

http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx

Also you will need to figure out how to manipulate the strings you read in. A resource for this:

http://msdn.microsoft.com/en-us/library/362314fe%28VS.71%29.aspx

good luck I'm sure you will get there

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜