IProgrammatori.it

Forum programmazione

Forum di supporto alla programmazione in generale e nei principali linguaggi.

It is currently Mon Mar 15, 2010 9:44 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [C] acquisizione nomi cartelle e file
PostPosted: Wed Jun 24, 2009 7:00 pm 
Newbies
Joined: Wed Jun 24, 2009 6:55 pm
Posts: 1

Ciao a tutti,
volevo sapere se era possibile (lavorando con OS Windows XP) acquisire in linguaggio C il nome di una lista di cartelle all'interno di un altra e il nome di altri file all'interno delle singole cartelle.
Provo a spiegarmi meglio...io ho la seguente cartella principale:

C:\clienti

all'interno di questa cartella ho altre cartelle con vari nominativi:

C:\clienti\rossi mario
C:\clienti\bianchi carlo
C:\clienti\verdi gianni
...
...


e all'interno di ogni cartella ho dei file:

C:\clienti\rossi mario\CI.txt
C:\clienti\rossi mario\CF.txt
C:\clienti\rossi mario\libretto.jpg
...
...

ebbene vorrei acquisire solo i nomi e avere come output a video qualcosa del genere:

nome cliente: rossi mario
file associati: CI.txt , CF.txt, libretto.jpg

...

qualcuno di voi saprebbe indicarmi come fare per importare i nomi e scorrere all'interno delle varie cartelle???

un grazie anticipato...Fabio


Offline
 Profile E-mail  
 Post subject: Re: [C] acquisizione nomi cartelle e file
PostPosted: Thu Jun 25, 2009 1:15 pm 
Moderatore
Joined: Tue Jun 27, 2006 11:52 am
Posts: 259

Code:
/*
* A function to list all contents of a given directory
* author: Danny Battison
* contact: gabehabe@hotmail.com
*/

#include <dirent.h> // directory header
#include <cstdio>

void listdir (const char *path)
{
    // first off, we need to create a pointer to a directory
    DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
    pdir = opendir (path); // "." will refer to the current directory
    struct dirent *pent = NULL;
    if (pdir == NULL) // if pdir wasn't initialised correctly
    { // print an error message and exit the program
        printf ("\nERROR! pdir could not be initialised correctly");
        return; // exit the function
    } // end if

    while (pent = readdir (pdir)) // while there is still something in the directory to list
    {
        if (pent == NULL) // if pent has not been initialised correctly
        { // print an error message, and exit the program
            printf ("\nERROR! pent could not be initialised correctly");
            return; // exit the function
        }
        // otherwise, it was initialised correctly. let's print it on the console:
        printf ("%s\n", pent->d_name);
    }

    // finally, let's close the directory
    closedir (pdir);
}

/** EXAMPLE USAGE **/
int main ()
{
    listdir ("C:\\");
    return 0;
}


ecco un esempio ben commentato

_________________
per qualsiasi consiglio, problema riguardante prodotti microsoft visitate http://www.windowsolution.org


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 2 hours


 Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: