下面是代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace GPA_Calculator
{
    public partial class Retrieve : Form
    {
        //Declare Global vatiables        const char DELIM = '*';//declare delimiter between data fields
        const string FILENAME = @"GPA.txt";//declare location of data file
        string name, nameEntered;//declare user name and the name user entered
        string recordIn;// declare record read from data file
        string[] fields;//declare array of fields for each record
        string adminNO, adminNOEntered;//declare the adminNO
        string moduleName, moduleNO, grade;
        double moduleCredit;
        int lgFailure; //declare the login failure times        FileStream outFile;//file stream objected
        StreamReader reader;//Stream reader objected
        public Retrieve()
        {
            InitializeComponent();
        }        private void retrieveBtn_Click(object sender, EventArgs e)
        {
            bool success = false;
            // Create a file stream object to open a file called FILENAME
            outFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
            // Create a stream reader with the outFile File Stream object
            reader = new StreamReader(outFile);            try
            {
                do
                {
                    // Use stream reader to read the file
                    recordIn = reader.ReadLine();
                    //Split up a string using the delimter and
                    //store them to an array called field
                    fields = recordIn.Split(DELIM);                    // Pick the content from the array 
                    nameEntered = nameTB.Text;
                    adminNOEntered = adminTB.Text;
                    name = fields[0];
                    adminNO = fields[1];
                    moduleName = fields[2];
                    moduleNO = fields[3];
                    moduleCredit = Convert.ToDouble(fields[4]);
                    grade = fields[5];                    //Check the if the name and pin entered is correct
                    if (nameEntered == name && adminNOEntered == adminNO)
                    {
                        MessageBox.Show("Information is found!");                        // Login successfully,break
                        success = true;
                        break;
                    }
                } while (true);
                //show the result in the label
                    showLbl.Text = "Student's Name: " + name + Environment.NewLine.ToString()+"Admin NO.: " + adminNO +
                    Environment.NewLine.ToString()+"Module Name: "
                    + moduleName + Environment.NewLine.ToString()+ "Module NO.: " + moduleNO
                    + Environment.NewLine.ToString() + "Grade is: " + grade;            }
            catch (Exception)
            {
                // Login fail,login failure time + 1
                MessageBox.Show("Please check your name and admin NO.!");
                lgFailure++;                //clear the text boxes
                nameTB.Clear();
                adminTB.Clear();
            }
            //close the reader and the outfile
            reader.Close();
            outFile.Close();            // if login failure time>=3, form closed
            if (lgFailure >= 3)
            {
                MessageBox.Show("You have exceeded the number of tries, bye!!");                this.Close();
            }
        }        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}程序的要求是从file里面读出所有的一个学生的成绩记录 比如 这个文本文件里面有John*110038A*PMP*EG1745*4*B
Lucy*110042A*Data communacation*EG1755*4*A
John*110038A*Infocomm System*EG1756*4*B
Jim*110056A*Graphic Design*4*A点击按钮 Retrieve 输入姓名和学号 就要把所有关于这个学生的信息显示在一个label上比如我输入 John 110038A 就要显示
Name:John   AdminNO.110038A
PMP   EG1745    4     B
Infocomm System   EG1756   4  B
我现在做的是只能读一行数据, 我想知道怎么把全部数据都读出来大侠们帮帮忙了 小弟不胜感激!!

解决方案 »

  1.   

    读取每一行,以*号split分成数组,然后自己判断把
      

  2.   

    怎么读取每一行啊?是ReadToEnd吗?
    我是刚学习C#,有好多东西都很陌生。楼上大侠,拜托能给个例子吗?
      

  3.   

    将下面这段注销再看一下:
    if (nameEntered == name && adminNOEntered == adminNO)
      {
      MessageBox.Show("Information is found!");  // Login successfully,break
      success = true;
      break;
      }
      

  4.   

    刚才又看了下,你要取所有的数据最好将数据封装在一个集合里,
    如:
      name = fields[0];
      adminNO = fields[1];
      moduleName = fields[2];
      moduleNO = fields[3];
      moduleCredit = Convert.ToDouble(fields[4]);
      grade = fields[5];
    ==>
      List<Student> list=new List<Student>();//注:这个要放在循环外面
      Student stu=new Student(); //这个要放在循环里面
      stu.name = fields[0];
      stu.adminNO = fields[1];
      stu.moduleName = fields[2];
      stu.moduleNO = fields[3];
      stu.moduleCredit = Convert.ToDouble(fields[4]);
      stu.grade = fields[5];
      list.Add(stu);
    再显示数据的时候想显示哪条就显示哪条
      

  5.   

    你既然可以将文件里面的内容读取出来,那么你就可以以/n <应该是/n有时候也可能是/n/n不是很记得了>去分割每一行,然后再去处理每一行的结果,你可以试着Debugger一下,监视你读取出来的文件。
      

  6.   

    我还是说一下整个程序吧就是要可以记录不同学生的GPA每个学生要可以记录不同科目的分数数据全部存在一个txt文件里面,便于以后检索检索的时候 输入一个学生的姓名和学号这个学生的所有记录全部显示出来最后还要求计算这个学生的最终GPA, 就是把此学生所有科目的分数求平均分