Jump to content
Sign in to follow this  
mastervlad

Ajutor problema

Recommended Posts

Am urmatoarea problema in c care imi spune care fisiere sunt in dir1 si NU sunt in dir2. Problema e urmatoarea:sa spunem ca dir1 are ca subdirector pe a si in a avem fisierul bbb, iar in dir2 am doar fisierul bbb. Mie o sa-mi spuna programul ca fisierul bbb se afla si in dir1 si in dir2, dar defapt nu se afla oarecum pe acelasi nivel, si in dir1 se afla in subdirectorul a. Eu vreau sa-mi spuna ca acest bbb din dir1 e diferit de cel din dir2, cum pot as putea sa modific ca sa mearga? Mersi.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/param.h>
#include <limits.h>
#include <string.h>
char *path2;
int dir2compare( char *path, char *findme ){
DIR *dir;
struct dirent *entry;
char spath[PATH_MAX] = "", spath2[PATH_MAX] = "";
if( !(dir = opendir( path))){ perror("opendir"); exit(1);}
for( entry = readdir( dir); entry; entry = readdir( dir)){
sprintf( spath, "%s/%s", path, entry->d_name);
if( entry->d_type == DT_REG){
if(strcmp(entry->d_name,findme)==0)
return 1;
}
if( entry->d_type == DT_DIR &&
(strcmp( ".", entry->d_name)) &&
(strcmp( "..", entry->d_name))){
dir2compare(spath,findme);
}
}
closedir( dir);
}



char dirfind( char *path, int *fgasit){
DIR *dir;
char *temp;
struct dirent *entry;
char spath[PATH_MAX] = "", spath2[PATH_MAX] = "";
if( !(dir = opendir( path))){
perror("opendir"); exit(1);}
for( entry = readdir( dir); entry; entry = readdir( dir)){
sprintf( spath, "%s/%s", path, entry->d_name);
if( entry->d_type == DT_REG){
if(dir2compare(path2, entry->d_name)==0) {
printf("[*] %s\n", spath); (*fgasit)++; }
}
if( entry->d_type == DT_DIR &&
(strcmp( ".", entry->d_name)) &&
(strcmp( "..", entry->d_name))){
dirfind(spath, fgasit);
}
}
closedir( dir);
return(0);
}
int main(int argc, char *argv[]){
int i = 0;
if (argc == 3){
path2=strdup(argv[2]);
dirfind(argv[1],&i);
printf(" %d fisiere nu corespund.\n", i);
}
}

Share this post


Link to post
Share on other sites

Uita-te in exemplul acesta:


#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

void listdir(const char *name, int level)
{
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;

do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s[%s]\n", level*2, "", entry->d_name);
listdir(path, level + 1);
}
else
printf("%*s- %s\n", level*2, "", entry->d_name);
} while (entry = readdir(dir));
closedir(dir);
}

int main(void)
{
listdir(".", 0);
return 0;
}

care parcurge in mod recursiv toate fisierele si directoarele dintr-un director. Ai putea declara un sir global in care memorezi toate fisierele:


// declari aceasta variabila globala
// cuvantul [B]static[/B] sugereaza ca variabielele globale nu sunt vizibile in alte fisiere
static char fis_dir[100][100];
// ne trebuie si un index global pentru a itera in tabloul de mai sus
int index, index1;
// vom declara, de asemenea, si o variabila care va stoca rezultatele(fisierele gasite in ambele directoare)
static char rezultate[100][100];

iar ulterior il populezi cu toate fisierele din primul director:


#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

void listdir(const char *name, int level)
{
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;

do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s[%s]\n", level*2, "", entry->d_name);
listdir(path, level + 1);
}
[COLOR="#FF0000"] else {
printf("%*s- %s\n", level*2, "", entry->d_name);
strcpy(fis_dir[index++], entry->d_name);
}[/COLOR]
} while (entry = readdir(dir));
closedir(dir);
}

int main(void)
{
listdir(".", 0);
return 0;
}

Acum avem toate fisierele din primul director in tabloul fis_dir care este global si are index elemente dupa ce am rulat functia de mai sus.

Acum scriem o functie care cauta un sir in tablou:


int cauta_sir(const char sir[]) {
for(int i = 0; i < index; i++)
if(!strcmp(fis_dir[i], sir)
strcpy(rezultate[index1++], sir);
}

In aplicatii comerciale este descurajata folosirea variabilelor globale si statice. Le-am folosit deoarece un cod complet este relativ voluminos. In realitate ai declara o structura care contine respectivele variabile si ai transmite-o ca si parametru in functia de cautare.

Edited by Ganav

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...