Click here to Skip to main content
16,019,427 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Dear all,
I got a problem, that I can read only one record. The other record doesn't show.
The data in file is
001 
Phanny 
F 
Toul Kork 
077 95 39 95 
123abc 
Monday, January 09, 2012 
Tuesday, January 31, 2012 
good 

002
Thary
M
PP
012345678
asf 
Tuesday, January 10, 2012 
Tuesday, January 10, 2012 
good

And the reasult is
001 Phanny F Toul Kork 077 95 39 95 123abc Monday, January 09, 2012 Tuesday,January 31, 2012 good.
This result shows in ListView as below
ID Name Gender Address Phone Passport SartDate EndDate Decriptiom
001 Phanny F PP 077953995 123abc Mon... Tue... good

Here is my code
writing to a file
string tLine = "";
            StreamWriter objWriter = new StreamWriter("text.txt",true);
            string[] arrTest = new string[9];
            arrTest[0] = txtID.Text;
            arrTest[1] = txtName.Text;
            arrTest[2] = cmbGender.Text;
            arrTest[3] = txtAddress.Text;
            arrTest[4] = txtPhone.Text;
            arrTest[5] = txtPassport.Text;
            arrTest[6] = dateTPStartdate.Text;
            arrTest[7] = dateTPEndDate.Text;
            arrTest[8] = txtDescription.Text;
            //ind++;
            for (j = 0; j < 9; j++)
            {
                objWriter.WriteLine(arrTest[j]+" ");
            }
            objWriter.WriteLine();
            objWriter.Close();

Reading from a file
ListViewItem lis = new ListViewItem();

           FileInfo afile = new FileInfo("text.txt");
           if (afile.Exists)
           {
               StreamReader objReader = new StreamReader("text.txt", true);
               string textLine = "";
               do
               {
                   textLine = objReader.ReadLine();
                   lis.SubItems.Add(textLine);
               } while (objReader.Peek() != -1);

               listView1.Items.Add(lis);
               objReader.Close();
           }
           else {
               MessageBox.Show("There is no file");
           }


Please help me.
Thanks
Posted
Comments
phanny 2011 9-Jan-12 23:51pm    
I want all the data in file will show all in ListView and in the different record
Thanks

Use this way to check end of file.

C#
while ((textLine = objReader.ReadLine()) != null)
           {
                lis.SubItems.Add(textLine);
           }



Also you can try to use BinaryReader and BinaryWriter. It is better than StreamReader and StreamWriter for structural data.
 
Share this answer
 
Hi,

It will be easy to read record if you use delimiter for each record.

Please see MSDN
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900