ini文件内容如下:
[一号用户]  
姓名=张三
学号=3  
班级=一班  
年级=00  
[二号用户]  
姓名=李四
学号=83  
班级=一班  
年级=99 
[三号用户]  
姓名=王五
学号=63  
班级=一班  
年级=01
[三号用户]  
姓名=XX
学号=78  
班级=一班  
年级=04现在知道关键字姓名的值:name:string, 求一个取得该用户其余关键字值的函数。
即,知道姓名=王五, 现在要得到王五的其它信息。

解决方案 »

  1.   

    最后一个应该是四号用户吧,要不你这INI文件有问题function TForm1.GetInfoByName(Name: String): String;
    var
      IniFile:TIniFile;
      vMainList,vInfoList:TStrings;
      i:Integer;
    begin
      IniFile:=TIniFile.Create('D:\a.ini');
      vMainList:=TStringList.Create;
      try
        IniFile.ReadSections(vMainList);
        for i:=0 to vMainList.Count-1 do
        begin
          vInfoList:=TStringList.Create;
          try
            IniFile.ReadSectionValues(vMainList.Strings[i],vInfoList);
            if vInfoList.Values['姓名']=Name then
            begin
                vInfoList.Delete(vInfoList.IndexOfName('姓名'));
                Result:=vInfoList.Text;
                //ShowMessage(vInfoList.Text);
            end;
          finally
            vInfoList.Free;
          end;
        end;
      finally
        IniFile.Free;
        vMainList.Free;
      end;
    end;
    我已经测试过了的
      

  2.   

    我也来做作业type
      TStudent=packed record
        Name:string[10];
        No:string[3];
        Classes:string[4];
        Grade:string[2];
      end;
    function ReadStudent(s:string;var Student:TStudent):Boolean;
    var
      myini:TIniFile;
      List:TStringList;
      i:integer;
    begin
      List:=TStringList.Create;
      myini:=TIniFile.Create('.\abc.ini');
      myini.ReadSections(List);
      i:=0;
      while i<List.Count-1 do
      begin
        Student.Name:=myini.ReadString(List[i],'姓名','');
        if Pos(s,Student.Name)>0 then
        begin
          Result:=True;
          Student.No:=myini.ReadString(List[i],'学号','');
          Student.Classes:=myini.ReadString(List[i],'班级','');
          Student.Grade:=myini.ReadString(List[i],'年级','');
          i:=List.Count;
        end
        else Result:=False;
        inc(i);
      end;
      myini.Free;
      List.Free;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Student:TStudent;
      s:string;
    begin
      s:='王五';
      if ReadStudent(s,Student) then
      ShowMessage(Student.Name+#13#10+
        Student.No+#13#10+Student.Classes+#13#10+Student.Grade);
    end;