델파이2007. 11. 19. 23:52

다음 소스는 info2.txt 라는 파일을 불러와서 , 로 구분되어있는 문자열들을 StringGrid로 옮기는 작업을 하는것입니다.
보시다 시피 폼이 만들어질때 파일을 가져와서(파일 내용은 수검번호,이름,주민번호,학년,반으로 , 로 구분되어져 있습니다.) 일단 파일이 있는지 확인하고 ,를 찾아서 각 변수에 복사, 삭제를 반복하는 형식으로 되어있습니다. 아 힘들다.ㅜ.ㅡ

procedure TForm1.FormCreate(Sender: TObject);
var
  temp, st, files, bun, name, jumin, school, hak, ban:string;
  n, p:Integer;
  fh:TextFile;
begin
  n:=1;
  files:='info2.txt';

  StringGrid1.Cells[0,0]:='수검번호';
  StringGrid1.Cells[1,0]:='이름';
  StringGrid1.Cells[2,0]:='주민번호';
  StringGrid1.Cells[3,0]:='학년';
  StringGrid1.Cells[4,0]:='반';

  if not FileExists(files) then
  begin
    ShowMessage(files+'파일이 없습니다.');
    Exit;
  end;

  AssignFile(fh,files);

  Reset(fh);

  while not Eof(fh) do
  begin
    ReadLn(fh,temp);

    p:=Pos(',',temp);
    bun:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    name:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    jumin:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    p:=Pos(',',temp);
    school:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    if ComboBox1.Items.IndexOf(school)= -1 then
      ComboBox1.Items.Add(school);

    p:=Pos(',',temp);
    hak:=Copy(temp,1,p-1);
    Delete(temp,1,p);

    ban:=temp;

    StringGrid1.Cells[0,n]:=bun;
    StringGrid1.Cells[1,n]:=name;
    StringGrid1.Cells[2,n]:=jumin;
    StringGrid1.Cells[3,n]:=hak;
    StringGrid1.Cells[4,n]:=ban;

    StringGrid1.RowCount:= n+1;

    Inc(n);
  end;

  Caption:='건수 : '+IntToStr(n-1);
end;

Posted by Mons