Выкладываю исходный код
///////////////////////////////////////////////////// Это то что я заполняю
struct fnode { //структура для
char *filelist[100];
long countname;
long inode;
};
struct fnode myfiles[500];
long countfile = 0;
///////////////////////////////////////////////////// Это где я вызывю ф-ю заполнения
void printdir(char *dir,int depth){
DIR *dirpath;
struct dirent *entry;
struct stat statbuf;
if((dirpath = opendir(dir)) == NULL){
fprintf(stderr,"Не могу открыть каталог %s
",dir);
return;
}
chdir(dir);
while((entry = readdir(dirpath)) != NULL){
memset(&statbuf,0,sizeof(statbuf));
lstat(entry->d_name,&statbuf);
if(!S_ISDIR(statbuf.st_mode)){
initFileData(entry->d_name,statbuf.st_ino); // вот здес я заполняю глобальный массив Массив заполняется не так как надо
getData(entry->d_name,statbuf.st_ino); // для теста
printf("%*s %d %s - этот файл имеет %d жестких ссылок
",depth," ",statbuf.st_ino,entry->d_name,statbuf.st_nlink); //а здесь нормально все отображается
}
if(S_ISDIR(statbuf.st_mode)){
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue;
printf("%*s%s - это каталог
",depth," ",entry->d_name);
printdir(entry->d_name,depth+4);
}
}//endwhile
chdir("..");
closedir(dirpath);
}
///////////////////////////////////////////////////// Чем я заполняю
initFileData(entry->d_name,statbuf.st_ino);
getData(entry->d_name,statbuf.st_ino); // это так для теста Выходит так что имена файлов получаются какие то нетакие
///////////////////////////////////////////////////// Это ф-ии которые отображают заполненные данные (массивы)
printdir(topdir,0);
outData();
///////////////////////////////////////////////////////////////////////////////////
Здесь привожу полный текст исходного кода Пож-ста не поленитесь откомпильте В чем ошибка Программной ошибки так таковой я не вижу Ну никак не могу понять почему не так заполняется массив
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
struct fnode { //структура для
char *filelist[100];
long countname;
long inode;
};
struct fnode myfiles[500];
long countfile = 0;
char *filenames[100];
long count = 0;
void printdir(char *dir,int depth);
void printfiles();
void outData();
int main(int argc, char *argv[]){
char *topdir = ".";
if(argc >= 2) topdir = argv[1];
printf("Сканируем каталог %s и вычисляем имена файлов для inode.
",topdir);
printdir(topdir,0);
outData();
//printfiles();
return 0;
}
void printfiles(){
long i=0;
for(;i<countfile;i++){
printf("inode: %d
",myfiles.inode);
printf("Имена файлов: ");
long j=0;
for(;j<myfiles.countname;j++){
printf("'%s' ",myfiles.filelist[j]);
};
printf("
");
};
}
void initFileData(char *name, long inode){
int flag = 0;
if(countfile == 0) {
myfiles[0].inode = inode;
myfiles[0].countname = 1;
myfiles[0].filelist[0] = name;
countfile += 1;
} else {
if (countfile < 500){
long i=0;
for(;i<countfile;i++){
if(inode == myfiles.inode){
flag = 1;
if(myfiles.countname < 100) {
myfiles.filelist[myfiles.countname] = name;
myfiles.countname += 1;
break;
} else {
printf("
Сработало ограничение на количество имен файлов!
");
break;
}
}
};//endfor
if(!flag){
myfiles[countfile].inode = inode;
myfiles[countfile].countname = 1;
myfiles[countfile].filelist[0] = name;
countfile += 1;
}
} else {
printf("
Сработало ограничение на количество просканированных файлов!
");
return;
}
}//end else
}//end function
void getData(char *name, long inode){
filenames[count] = name;
count += 1;
}
void outData(){
long i=0;
for(;i<count;i++){
printf("%s
",filenames);
}
}
void printdir(char *dir,int depth){
DIR *dirpath;
struct dirent *entry;
struct stat statbuf;
if((dirpath = opendir(dir)) == NULL){
fprintf(stderr,"Не могу открыть каталог %s
",dir);
return;
}
chdir(dir);
while((entry = readdir(dirpath)) != NULL){
memset(&statbuf,0,sizeof(statbuf));
lstat(entry->d_name,&statbuf);
if(!S_ISDIR(statbuf.st_mode)){
initFileData(entry->d_name,statbuf.st_ino); // вот здес я заполняю глобальный массив Массив заполняется не так как надо
getData(entry->d_name,statbuf.st_ino);
printf("%*s %d %s - этот файл имеет %d жестких ссылок
",depth," ",statbuf.st_ino,entry->d_name,statbuf.st_nlink); //а здесь нормально все отображается
}
if(S_ISDIR(statbuf.st_mode)){
if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue;
printf("%*s%s - это каталог
",depth," ",entry->d_name);
printdir(entry->d_name,depth+4);
}
}//endwhile
chdir("..");
closedir(dirpath);
}
Пользователь решил продолжить мысль 04 Декабря 2009, 16:53:16:
У меня еще один вопрос возник Си подерживает ссылку?