-1

I want to read data from specific path but i the program read the first line only. my program gets data from user and save it int .txt file then i want to display all contents in between the delimiters.

input

1@aaa@bbbb@2@c@f@3@r@t

output

Id:1
Name:aaa
Address:bbbb

Id:2
Name:c
Address:f

Id:3
Name:r
Address:t

here is my code: main

FileStream fs = new FileStream(@"E:\New folder\a ", FileMode.Open);
StreamReader sd = new StreamReader(fs);
string s;
while (true)
{                            
    s = sd.ReadLine();
    field = s.Split(std.delimiter);
    std.ID = field[0];
    std.Name = field[1];
    std.Address = field[2];
    std.Display_data();
    sd.Close();
    fs.Close();
    break;
}

class

public void Display_data()
{
     Console.WriteLine(ID);
     Console.WriteLine(Name);
     Console.WriteLine(Address);    
}
VK_217
  • 9,965
  • 7
  • 34
  • 52
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – p.s.w.g Apr 22 '16 at 18:27
  • @p.s.w.g i edited it, please read it again. – Bassem Youssef Apr 22 '16 at 18:37
  • `break` exists the while-loop immediately after the first execution. Remove it and replace the loop condition with an appropriate test. – Olivier Jacot-Descombes Apr 22 '16 at 18:37

2 Answers2

1

You need to remove

break;

Edit while condition and move

sd.Close();
fs.Close();

out of the while cycle.
So your while cycle should look like this

while (!sd.EndOfStream)
{                            
    s = sd.ReadLine();
    field = s.Split(std.delimiter);
    std.ID = field[0];
    std.Name = field[1];
    std.Address = field[2];
    std.Display_data();
}
sd.Close();
fs.Close();
0

Read line,split using '@' and then read 3 line items and print out the lineitems.

using(StreamReader oReader = new StreamReader(@"E:\Newfolder\a\test.txt"))
{
    string [] sLineItems = oReader.ReadLine().Split('@');

    for(int i = 0;i < sLineItems.Length; i = i+3)
    {
          Console.WriteLine("Id:{0}",sLineItems[i]);
          Console.WriteLine("Name:{0}",sLineItems[i+1]);
          Console.WriteLine("Address:{0}",sLineItems[i+2]);
    }
}
VK_217
  • 9,965
  • 7
  • 34
  • 52