Registriert seit: 10. Aug 2007
609 Beiträge
FreePascal / Lazarus
|
AW: Daten schrittweise in Textdatei speichern
6. Apr 2011, 10:08
@himi: ja stimmt, diese konvertiererei dieser Urzeitunits macht mich total Kirre...
Aber ich hab hier noch eine andere Variante, die dich bestimmt interessiert
Delphi-Quellcode:
function SetFilePointer(hFile: Cardinal; lDistanceToMove: Longint; lpDistanceToMoveHigh: Pointer;
dwMoveMethod: Cardinal): Cardinal; stdcall; external kernel32 name 'SetFilePointer';
function FileExists(const FileName: string; const PFileSize: PInt64 = nil): Boolean; {$IFDEF Inln}inline;{$ENDIF}
var
myFile: Cardinal;
myFindData: TWin32FindData;
TheInt64: Int64;
begin
myFile := FindFirstFile(@FileName[1], myFindData);
Result := myFile <> INVALID_HANDLE_VALUE;
if Result then
begin
FindClose(myFile);
if PFileSize <> nil then
begin
TheInt64 := PFileSize^;
Int64Rec(TheInt64).Lo := myFindData.nFileSizeLow;
Int64Rec(TheInt64).Hi := myFindData.nFileSizeHigh;
PFileSize^ := TheInt64;
end;
end;
end;
function OpenFile(const FileName: string; var hFile: Cardinal; const PFileSize: PInt64 = nil): Boolean; {$IFDEF Inln}inline;{$ENDIF}
begin
Result := FileExists(FileName, PFileSize);
if Result then
begin
hFile := CreateFile(@FileName[1], GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0);
Result := hFile <> INVALID_HANDLE_VALUE;
end;
end;
function WriteFileStr(const hFile: Cardinal; const AStr: string): Boolean; {$IFDEF Inln}inline;{$ENDIF}
var
Size, BytesWritten: Cardinal;
begin
Size := Length(AStr)*SizeOf(Char);
Result :=
(WriteFile(hFile, AStr[1], Size, BytesWritten, nil)) and
(Size = BytesWritten);
end;
function AddTextToFile(const Text, FilePath: string): Boolean; {$IFDEF Inln}inline;{$ENDIF}
var
hFile: Cardinal;
fs: UInt64;
begin
Result := false;
if not OpenFile(FilePath, hFile, @fs) then
Exit;
if SetFilePointer(hFile, fs+Length(Text)*SizeOf(Char), nil, FILE_BEGIN) = fs+Length(Text)*SizeOf(Char) then
begin
SetEndOfFile(hFile);
Result :=
(SetFilePointer(hFile, fs, nil, FILE_BEGIN)=fs) and
WriteFileStr(hFile, Text);
end;
CloseHandle(hFile);
end;
|
|
Zitat
|