Jump to content

Whether a given file argument is a directory or not ?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Gordack

Gordack

    Newbie

  • Members
  • Pip
  • 4 posts
Hello guys.
Happy new year ! :)

I have a question about stat() function.

Im trying to check if a given file argument is a directory or not, using stat() function ?

I couldnt find any examples. Can u help me with this ?

I found a small example but it doesnt help me.

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>


int main(int argc, char *argv[])

{

	struct stat buf;

	

	stat(argv[1],&buf);

	

	exit(0);


}


#2
Freedom Doc

Freedom Doc

    Newbie

  • Members
  • Pip
  • 8 posts

Gordack said:

Hello guys.
Happy new year ! :)

I have a question about stat() function.

Im trying to check if a given file argument is a directory or not, using stat() function ?

I couldnt find any examples. Can u help me with this ?

I found a small example but it doesnt help me.

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>


int main(int argc, char *argv[])

{

	struct stat buf;

	

	stat(argv[1],&buf);

	

	exit(0);


}

After doing your stat() call, you can do

bool is_dir = buf.st_mode & S_IFDIR;

(Assuming C++ and that you have included <sys/stat.h>)
Then is_dir is true if and only if argv[1] is a directory.