LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with directories & files (http://www.linuxquestions.org/questions/programming-9/need-help-with-directories-and-files-53879/)

GoboFraggle 04-07-2003 02:46 PM

Need help with directories & files
 
How can I programmatically get info about directories and files such as current directory or where is a file located? I am using C/C++ on a Mandrake 9.0 system. I haven't had any luck looking this up on my own so am forced to seek your kind assistance. Thanks

Gobo Fraggle

Tinkster 04-07-2003 03:25 PM

Programming language, Operating System? :)

Cheers,
Tink

GoboFraggle 04-07-2003 03:38 PM

I'm programming in C/C++ using KDev.3 on a Mandrake 9.0 system. Guess I forgot that important bit of info.

Gobo Fraggle

Tinkster 04-08-2003 03:19 PM

Look at QDir & QFileinfo ... ;)

$QTDIR/doc/html/index.html

Cheers,
Tink

champ 04-09-2003 06:04 AM

Here is an example on a directory print:

Code:

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/types.h>

void printdir()
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;

if ( (dp = opendir(dir)) == NULL)
{
    fprintf(stderr, "cannot open directory: %s\n", dir);
      return;
}

while ( (entry = readdir(dp)) != NULL)
{
      lstat(entry->d_name, &statbuf);
      if (S_ISDIR(statbuf.st_mode))
      {
              /* Found a directory, but ignore . and .. */
                if (strcmp(".", entry->d_name) == 0 ||
                        strcmp("..", entry->d_name) == 0)
                            continue;

                      printf("%*s%s/\n", depth, "", entry->d_name);
                      /* recurse at a new indent level */
                        printDir(entry->d_name, depth+4);
                }
                else
                        printf("%*s%s\n", depth, "", entry->d_name);
        }
}

chdir("..");
closedir(dp);

}

Look at man pages for opendir, readdir, chdir......


All times are GMT -5. The time now is 02:30 AM.