Recent

Author Topic: Size of textfile  (Read 1798 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Size of textfile
« on: March 16, 2020, 11:37:18 am »
Can somebody remind me please what the approved way of getting the current size of a currently-opened textfile in bytes is? Or at a pinch in chars?

I'd prefer to do this while it is still opened in case it's moved elsewhere by something (e.g. hooked onto an OS-level filesystem notification) as soon as it's closed.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Size of textfile
« Reply #1 on: March 16, 2020, 01:12:25 pm »
Do you mean something like that?
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses sysutils;
  4.  
  5. var
  6.   F: THandle;
  7.   Size: Int64;
  8.  
  9. begin
  10.   F := FileOpen('test.txt', fmOpenRead or fmShareDenyNone);
  11.   Size := FileSeek(F, 0, fsFromEnd);
  12.   FileClose(F);
  13.  
  14.   WriteLn('File test.txt has ', Size, ' bytes.');
  15.  
  16.   ReadLn;
  17. end.
  18.  
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11445
  • FPC developer.
Re: Size of textfile
« Reply #2 on: March 16, 2020, 01:21:32 pm »
I'd try to setup a tfilerec ("file") handle with the existing textrec's handle.

Or use OS specific routines.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: Size of textfile
« Reply #3 on: March 16, 2020, 01:28:40 pm »
I'd try to setup a tfilerec ("file") handle with the existing textrec's handle.

Or use OS specific routines.

Thanks for that, that's more or less what I was thinking had to be done but I thought I'd check.

(Slightly later)

Code: [Select]
Flush(txtFile);
if FPFStat(txtFile, info) then
  result += ', ' + IntToStr(info.st_size) + ' bytes (' + IntToStr(lineCount) + ' lines)'
else
  result += ', ' + IntToStr(lineCount) + ' lines'

It appears that FPFStat() can process a text file as a parameter, however the explicit Flush() turns out to be mandatory otherwise stuff that is still locally-buffered doesn't appear in the total.

MarkMLl
« Last Edit: March 16, 2020, 01:45:03 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Size of textfile
« Reply #4 on: March 16, 2020, 03:33:49 pm »
Code: Pascal  [Select][+][-]
  1.   function GetFileSize(aFileName : string):Int64;
  2.   var
  3.     lFileStream : TFileStream = nil;
  4.   begin
  5.     Result := -2; // file not found
  6.     if FileExists(aFileName) then begin
  7.       try
  8.         lFileStream := TFileStream.Create(aFileName, fmOpenRead or fmShareDenyNone);
  9.         Result := lFileStream.Size;
  10.         lFileStream.Free;
  11.       except
  12.         Result := -1; // error
  13.       end;
  14.     end;
  15.   end;

 

TinyPortal © 2005-2018