Q&A

  • [파일 쓰기]스트링그리드에서 text파일로..
전에 파일읽어서(tab 구분자가 있는) stringgrid에 입력하기를 물어봤었는데..

김하늘님께서 잘 가르쳐 주셨더군요..

정말 감사합니다. 많은 도움이 되었습니다.

근데.. 반대로 질문을 하게 되었군요..

stringgrid-> text로 쓰기해야 하는데.. 나름대로 구현을 해보니깐.. 파일이 깨져있더군요..

소스를 다음과 같습니다.

많은 고견부탁드립니다.

==================================================================================

procedure Tfcti9000.bb_CreatErrFileClick(Sender: TObject);

var

BackupName: string;

FileHandle: Integer;

StringLen : Integer;

StringVal : String;

X: Integer;

Y: Integer;

begin

if SaveDialog1.Execute then

begin

if FileExists(SaveDialog1.FileName) then

begin

BackupName := ExtractFileName(SaveDialog1.FileName);

BackupName := ChangeFileExt(BackupName, '.BAK');

if not RenameFile(SaveDialog1.FileName, BackupName) then

raise Exception.Create('Unable to create backup file.');

end;

FileHandle := FileCreate(SaveDialog1.FileName);

{ Write out the number of rows and columns in the grid. }

FileWrite(FileHandle,

sgr_display.ColCount, SizeOf(sgr_display.ColCount));

FileWrite(FileHandle,

sgr_display.RowCount, SizeOf(sgr_display.RowCount));

for X := 0 to sgr_display.ColCount - 1 do begin

for Y := 0 to sgr_display.RowCount - 1 do begin

{ Write out the length of each string, followed by the string itself. }

StringVal := sgr_display.Cells[X,Y];

StringLen := Length(sgr_display.Cells[X,Y]);

FileWrite(FileHandle, StringLen, SizeOf(StringLen));

FileWrite(FileHandle, StringVal, Length(StringVal));

end;

end;

FileClose(FileHandle);

end;



end;

==================================================================================



1  COMMENTS
  • Profile
    김하늘 2001.06.22 05:02
    일단 팁자료실을 참고 하시고요....

    구체적으로 텍스트 파일이 어떤 형태로 만들어져야 하는지 파악이 안된

    관계로 답변을 구체화 할순 없겠네여...

    일단 csv 형태로 맹그는 방법으로다 코딩합니다...

    아래 win32 api로 포팅된 평션이 아니라 좀더 간단한 파일입출력으로다...

    코딩 간단하니까 이해가 파악 오실겁니다...



    그럼 즐프하세요



    procedure TForm1.Button1Click(Sender: TObject);

    var

    ffin: TextFile;

    StringVal: String;

    X, Y: Integer;

    begin

    AssignFile(ffin, 'c:파일명.csv');

    rewrite(ffin);



    for Y := 0 to sgr_display.RowCount - 1 do begin

    for X := 0 to sgr_display.ColCount - 1 do begin

    StringVal := sgr_display.Cells[X, Y];

    if x = (sgr_display.ColCount-1) Then WriteLN(ffin, StringVal)

    else begin

    StringVal := StringVal + ',';

    Write(ffin, StringVal);

    end;

    end;

    end;

    CloseFile(ffin);

    end;





    김민경 wrote:

    > 전에 파일읽어서(tab 구분자가 있는) stringgrid에 입력하기를 물어봤었는데..

    > 김하늘님께서 잘 가르쳐 주셨더군요..

    > 정말 감사합니다. 많은 도움이 되었습니다.

    > 근데.. 반대로 질문을 하게 되었군요..

    > stringgrid-> text로 쓰기해야 하는데.. 나름대로 구현을 해보니깐.. 파일이 깨져있더군요..

    > 소스를 다음과 같습니다.

    > 많은 고견부탁드립니다.

    > ==================================================================================

    > procedure Tfcti9000.bb_CreatErrFileClick(Sender: TObject);

    > var

    > BackupName: string;

    > FileHandle: Integer;

    > StringLen : Integer;

    > StringVal : String;

    > X: Integer;

    > Y: Integer;

    > begin

    > if SaveDialog1.Execute then

    > begin

    > if FileExists(SaveDialog1.FileName) then

    > begin

    > BackupName := ExtractFileName(SaveDialog1.FileName);

    > BackupName := ChangeFileExt(BackupName, '.BAK');

    > if not RenameFile(SaveDialog1.FileName, BackupName) then

    > raise Exception.Create('Unable to create backup file.');

    > end;

    > FileHandle := FileCreate(SaveDialog1.FileName);

    > { Write out the number of rows and columns in the grid. }

    > FileWrite(FileHandle,

    > sgr_display.ColCount, SizeOf(sgr_display.ColCount));

    > FileWrite(FileHandle,

    > sgr_display.RowCount, SizeOf(sgr_display.RowCount));

    > for X := 0 to sgr_display.ColCount - 1 do begin

    > for Y := 0 to sgr_display.RowCount - 1 do begin

    > { Write out the length of each string, followed by the string itself. }

    > StringVal := sgr_display.Cells[X,Y];

    > StringLen := Length(sgr_display.Cells[X,Y]);

    > FileWrite(FileHandle, StringLen, SizeOf(StringLen));

    > FileWrite(FileHandle, StringVal, Length(StringVal));

    > end;

    > end;

    > FileClose(FileHandle);

    > end;

    >

    > end;

    > ==================================================================================

    >