Recent

Author Topic: Get file size  (Read 2362 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Get file size
« on: March 03, 2021, 11:02:42 am »
Hello!

I used files a little time ago, so I forgot how to, then I tried it like that:

Code: Pascal  [Select][+][-]
  1. var
  2.   path: string;
  3.   sFile: file;
  4. begin
  5.  
  6.       writeln('Write here the location of the file to be checked!');
  7.       Write('>');
  8.       Readln(path);
  9.       Assignfile(sFile, path);
  10.       Reset(sFile);
  11.       writeln(IntToStr(FileSize(sfile)), ' bytes');
  12.       readln;
  13. end.
  14.  

and it gives runtime error on Reset(sFile); .

Handoko

  • Hero Member
  • *****
  • Posts: 5151
  • My goal: build my own game engine using Lazarus
Re: Get file size
« Reply #1 on: March 03, 2021, 11:04:41 am »

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Get file size
« Reply #2 on: March 03, 2021, 11:18:49 am »
It still gives the "unhandled issue occured" and "access denied"
but when I started Lazarus as Administrator it stays say.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Get file size
« Reply #3 on: March 03, 2021, 11:19:40 am »
And closes the window immediately, so it was hard to read :D

speter

  • Sr. Member
  • ****
  • Posts: 349
Re: Get file size
« Reply #4 on: March 03, 2021, 11:50:32 am »
Jake012345,

Since your file variable is of type "file", I think you need to specify the record size in reset(); try
Code: Pascal  [Select][+][-]
  1. reset(sfile,1);
which specifies the record is a byte;

Alternatively, type your file variable as "file of byte".

Also don't forget to close the file:
Code: Pascal  [Select][+][-]
  1. closefile(sfile);

cheers
S.
« Last Edit: March 03, 2021, 11:54:53 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: Get file size
« Reply #5 on: March 03, 2021, 11:54:21 am »
Code: Pascal  [Select][+][-]
  1.       writeln(IntToStr(FileSize(sfile)), ' bytes');
Whoa! It’s also been quite a while since you’ve used write/writeLn? You can just pass an integer value to writeLn, no need for intToStr. ;)

It still gives the "unhandled issue occured" and "access denied"
but when I started Lazarus as Administrator it stays say.
Well, these exceptions aren’t raised without a reason: You have to be authorized to access every component along the entire specified path. Maybe, for debugging insert a writeLn for getCurrentDir and path?

Try this: https://www.freepascal.org/docs-html/rtl/system/filesize.html
Note, this returns the number of “records” and a record for an untyped file is by default 128 Bytes, unless you do reset(sFile, 1); (so 1 defines a record’s size in Bytes).
Yours Sincerely
Kai Burghardt

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Get file size
« Reply #6 on: March 03, 2021, 12:11:13 pm »
how tiring is that ....
But thanks! finally working.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6685
Re: Get file size
« Reply #7 on: March 03, 2021, 11:21:30 pm »
Check my ProjectShare site and share your content: FPCShare

One moment please. You're making absolute beginner mistakes but claiming to run a project-sharing site which is relevant to the community?

Hadjab 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

dbannon

  • Hero Member
  • *****
  • Posts: 2793
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Get file size
« Reply #8 on: March 04, 2021, 12:06:35 am »
Check my ProjectShare site and share your content: FPCShare

Hmm, it certainly looks good. But has a spelling mistake in the opening paragraph, it has underlined text that is not a link and, worst of all, a link to Facebook.

Not a good start IMHO.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Get file size
« Reply #9 on: March 04, 2021, 10:04:37 am »
yes, it's mainly for other people to share their better projects, that helps to the beginners like me

(just because I didnt find any places like that)

 

TinyPortal © 2005-2018