LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2003, 02:46 PM   #1
GoboFraggle
LQ Newbie
 
Registered: Jan 2003
Posts: 10

Rep: Reputation: 0
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

Last edited by GoboFraggle; 04-07-2003 at 03:34 PM.
 
Old 04-07-2003, 03:25 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Programming language, Operating System? :)

Cheers,
Tink
 
Old 04-07-2003, 03:38 PM   #3
GoboFraggle
LQ Newbie
 
Registered: Jan 2003
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 04-08-2003, 03:19 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Look at QDir & QFileinfo ... ;)

$QTDIR/doc/html/index.html

Cheers,
Tink

Last edited by Tinkster; 09-08-2005 at 02:37 AM.
 
Old 04-09-2003, 06:04 AM   #5
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
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......

Last edited by champ; 04-09-2003 at 06:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 03:22 PM
C and reading files & directories problem climbingmerlin Programming 4 10-12-2005 02:25 PM
files and directories thanhVic Linux - Newbie 1 02-27-2005 04:04 PM
set file permissions for 100 directories & its files cevjr Linux - Security 6 01-06-2005 10:23 AM
accessing files & directories - a really simple question hildog Linux - Newbie 6 10-12-2003 06:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration