큰꼼의 세상

텍스트 파일을 TStringList를 이용해 읽고/쓰기


function LoadFile(const FileName: TFileName): string;
var
	StringList: TStringList;
begin
	StringList := TStringList.Create;
	
    try
		StringList.LoadFromFile(FileName);
		Result := StringList.Text;
	finally
		StringList.Free;
	end;
end;

procedure SaveFile(const FileName: TFileName; const S: String);
var
	StringList: TStringList;
begin
	StringList := TStringList.Create;
	
    try
		StringList.Text := S;
		StringList.SaveToFile(FileName);
	finally
		StringList.Free;
	end;
end;




텍스트 파일을 TFileStream를 이용해 읽고/쓰기

function LoadFile(const FileName: TFileName): string;
var
	Stream: TFileStream;
begin
	Stream := TFileStream.Create(FileName,
	fmOpenRead or fmShareDenyWrite);

	try
		SetLength(Result, Stream.Size);
		Stream.Read(Pointer(Result)^, Stream.Size);
	finally
		Stream.Free;
	end;
end;

procedure SaveFile(const FileName: TFileName; const S: string);
var
	Stream: TFileStream;
begin
	Stream := TFileStream.Create(FileName, fmCreate);

	try
		Stream.Write(Pointer(S)^, Length(S));
	finally
		Stream.Free;
	end;
end;