Recent

Author Topic: Get file size  (Read 1110 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • 'The knowledge is the key.'
    • FPCProjectShare
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); .
Check my ProjectShare site and share your content: FPCShare

Handoko

  • Hero Member
  • *****
  • Posts: 4209
  • 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
  • 'The knowledge is the key.'
    • FPCProjectShare
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.
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • 'The knowledge is the key.'
    • FPCProjectShare
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
Check my ProjectShare site and share your content: FPCShare

speter

  • Full Member
  • ***
  • Posts: 191
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. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • 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
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Get file size
« Reply #6 on: March 03, 2021, 12:11:13 pm »
how tiring is that ....
But thanks! finally working.
Check my ProjectShare site and share your content: FPCShare

MarkMLl

  • Hero Member
  • *****
  • Posts: 2508
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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and 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: 1373
    • 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 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • 'The knowledge is the key.'
    • FPCProjectShare
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)
Check my ProjectShare site and share your content: FPCShare

 

TinyPortal © 2005-2018