题目是:利用有类型文件保存学生的成绩,可以输入学生的学号、姓名以及3门功课的成绩,浏览后删除数据。
用到两个groupbox,第一个中有5个label(学号、姓名、语文、外语、数学)、5个edit和两个按钮(新记录、添加),第二个中有一个listbox1和一个按钮(删除)。
{}中的是我的问题,请按序号回答,谢谢了。//后的注释是我的理解unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  type
    studentrecord=record
      xh,xm:string[6];
      yw,sx,wy:integer;
    end;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
  t:studentrecord;
  f:file of studentrecord;
begin
  assignfile(f,'c:\my documents\文件.dat');
  if fileexists('c:\my documents\文件.dat') then
    reset(f)     
  else
    rewrite(f);
  while not eof(f) do
    begin
      read(f,t);
      listbox1.Items.Add(t.xh+'  '+t.xm+'  '+inttostr(t.yw)+'  '+inttostr(t.wy)+'  '+inttostr(t.sx));
    end;
  closefile(f);
  button3.Enabled:=false;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.Text:='';
  edit2.Text:='';
  edit3.Text:='';
  edit4.Text:='';
  edit5.Text:='';
end;procedure TForm1.Button2Click(Sender: TObject);
var
  t:studentrecord;
  f:file of studentrecord;
  size:integer;
begin
  t.xh:=edit1.text;
  t.xm:=edit2.text;
  t.yw:=strtoint(edit3.Text);
  t.wy:=strtoint(edit4.Text);
  t.sx:=strtoint(edit5.Text);
  assignfile(f,'c:\my documents\文件.dat');
  reset(f);
  size:=filesize(f);
  seek(f,size);
  write(f,t);              {Q1: 为什么用writeln不行呢?}
  listbox1.Items.Clear; 
  seek(f,0);               {Q2: 文件的指针是从0开始吗?}
  while not eof(f) do      {Q3: 循环的条件是什么?是不是指针递增?}
    begin
      read(f,t);           //将文件读到t中
      listbox1.Items.Add(t.xh+'  '+t.xm+'  '+inttostr(t.yw)+'  '+inttostr(t.wy)+'  '+inttostr(t.sx));
    end;
  closefile(f);
end;procedure TForm1.Button3Click(Sender: TObject);
var
  pos:integer;
  t:studentrecord;
  f:file of studentrecord;
begin
  pos:=listbox1.ItemIndex; //The ItemIndex of the first item in the list box is 0. If no item is selected, the value is -1
  assignfile(f,'c:\my documents\文件.dat');
  reset(f);
  seek(f,pos+1); 
  while not eof(f) do     {Q4: 请把这个循环的意思详细解释一下,谢谢!}
    begin
      read(f,t);      
      seek(f,pos);
      pos:=pos+1;
      write(f,t);         {Q5: 写的时候是否清除了它后面的没有被它覆盖的位置的数据呢?}
      seek(f,pos+1); 
    end;
  seek(f,pos);            {Q6: 指针指向什么位置了?}
  truncate(f);            {Q7: 是否包括当前位置呢?比如现在是3,截去的时候包括3吗?}
  seek(f,0);
  listbox1.Items.Clear;
  while not eof(f) do
    begin
      read(f,t);
      listbox1.Items.Add(t.xh+'  '+t.xm+'  '+inttostr(t.yw)+'  '+inttostr(t.wy)+'  '+inttostr(t.sx));
    end;
  closefile(f);
end;procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if listbox1.ItemIndex>-1 then   
    button3.Enabled:=true
  else
    button3.Enabled:=false;
end;
                         {Q8: 生成的文件中显示的内容跟listbox中的内容怎么不一样啊?像是乱码!}end.我的这本书是很折磨人的。像是文件指针都没有提到,fileexists、filesize 根本没有讲过就直接拿出来了,也没有任何解释。windows api也没提到过。还有很多根本就没讲过就直接拿出来用,搞得我头都大了,最可恶的是前言中写着“本书采用一种全新的教学方法,在学习delphi之前,不需要先学任何程序语言。”多亏我手边有本pascal的书,有些地方看了它才弄懂。看来看完这本书后我应该学一下pascal了!

解决方案 »

  1.   

    我记得以前做过一个实验,文件的最后一个字节再next,eof就为真,
    最前一个字节再prev,bof就为真。
      

  2.   

    q1: 在这里因为文件类型是自己定义的每一记录都有个长度而WRITELN是读一行,只适用于按文本方式进行读写(READLN),故在此你用WRITELN进行写文件是错误的。
    Q2:是的,文件开始读的时候应该从0开始。
    Q3:条件就是在文件没有结束时进行读写,文件指针自动增加
    q4: (意思是如果文件没有结束就执行它里面的化码),不过因为你打开的文件的方式不对如果你想写文件只能用REWRITE和APPEND, 前者是新建一个文件,如果文件存在则原来文件的内容将被不存在,而APPEND则是追加在已存文件的末尾如果文件不存在则以出错而终止!
    q5:会的,但你打开文件的方式不对!!!!!!!
    q6:指针指向POS位置(记住:文件指针是以0始的)