directory listings in programs

Thomas Stivers stivers_t at tomass.dyndns.org
Sat May 15 07:59:58 EDT 2004


-----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-----




More information about the Speakup mailing list