directory listings in programs

Joseph C. Lininger jbahm at pcdesk.net
Sun May 16 07:59:16 EDT 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes you are correct. Calling stat is more efficient than opening a file. The 
reason? To stat a file, the OS simply needs to look up information in the 
filesystem structures and return it. To open a file, the system actually has 
to allocate a file handle, associate it with a stream, etc. etc. etc. 
Needless to say, less work is involved for stat.
- ---
Joseph C. Lininger
jbahm at pcdesk.net
note, the following is used for automated processing. Please leave in tact 
if quoting me in a reply.
Verification: 5eab38a77ac40416e075be8f50607ff7
- ----- Original Message ----- 
From: "Thomas Stivers" <stivers_t at tomass.dyndns.org>
To: <speakup at braille.uwo.ca>
Sent: Saturday, May 15, 2004 5:59 AM
Subject: Re: directory listings in programs


> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On May 15 2004  1:35 AM, Gregory Nowak wrote:
>> Hi all.
>>
>> I am writing a c program under gnu/Linux, in which I need to know if a
>> file of a particular name exists. The only way I can think of checking
>> for this is to attempt to open a file of a given name in read-only
>> mode. If it opens, the file exists, if the FILE stream is NULL, the
>> file doesn't exist. However, doing this for a good number of files
>> would be inefficient in my opinion. So, I was wondering if there is a
>> function which will simply tell me if a file of a given name exists or
>> not, sort of like calling ls, although I don't actually want to be
>> literally doing that either from within the program? Thanks.
>
> It does a lot more than you're asking for, but I think the stat system
> call is what you're after. Check out man 2 stat for details. If I have
> understood things corectly stat'ing a file is more efficient than opening
> it.
>
> - -- 
> "Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are,
> by definition, not smart enough to debug it." - Brian W. Kernighan
>
> Thomas Stivers e-mail: stivers_t at tomass.dyndns.org
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (GNU/Linux)
>
> iD8DBQFApgY+5JK61UXLur0RAoJzAJ9AWfQURyehNJgT0lq+iXq/M3tbowCfamaI
> 5jll+3k7QEKCUNbW47bYyVk=
> =f0t1
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Speakup mailing list
> Speakup at braille.uwo.ca
> http://speech.braille.uwo.ca/mailman/listinfo/speakup
>
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3

iQA/AwUBQKdXkSenap9Jqj2wEQLS4QCfRWftRwGLlT5uryiBMlxXmQN5ZLgAoIUd
wGh1vbbKHsdhI3U2rQ9UEtqz
=6o2b
-----END PGP SIGNATURE-----





More information about the Speakup mailing list