Я работаю над заданием по программированию c и испытываю некоторые трудности с пониманием. Я создал функцию, которая должна создавать папку или файл (только со строкой.) Если новый объект является папкой, я хочу ввести количество субфайлов, рекурсивно.
Вопрос: Как получить доступ к подфайлам в случае, если я хочу работать с данными второго файла (только строка)? (а также доступ к подпапкам).
Структура файла:
typedef struct {
char *name; //name of File
char type; // 'f' - data file, 'd'-folder.
union {
char data[5]; //f -80 chars array for content of file.
struct { //d -arry of pointers to 'File' Files.
struct File ** files;
unsigned int size;
}folder;
}content;
}File;
Функция:
File * newFile(char type)
{
File *pfile = (File*)malloc(sizeof(File)); //creating new File
if (!pfile) {
printf("No memory for new file\n");
exit(1);
}
if (type != 'f' || type != 'd') { // in case of wrong input of type.
while(type != 'f' && type !='d'){
printf("Wrong input,try again: ");
fseek(stdin, 0, SEEK_END);
type =getchar();
}
}
printf("Enter File name: ");
fseek(stdin, 0, SEEK_END);
char name[NAME];
gets(name);
pfile->name = name;
if (type == 'f') { // New File is 'folder'
pfile->type = 'f';
//fseek(stdin, 0, SEEK_END);
printf("Enter number of files in %s: ",pfile->name);
scanf(" %d", &pfile->content.folder.size);
if (pfile->content.folder.size == 0) { // Zero files in the folder
pfile->content.folder.files = NULL; // No more files / empty folder
}
pfile->content.folder.files = (File**)malloc(pfile->content.folder.size * sizeof(File));
if (!pfile->content.folder.files) {
printf("No memory for sub files\n");
exit(2);
}
for (int i = 0; i < pfile->content.folder.size; ++i) { // creates 'size' file per folder
fseek(stdin, 0, SEEK_END);
printf("(%d):Enter type of file (d-folder,f-file): ", i + 1);
pfile->content.folder.files[i] = newFile(getchar());
}
return pfile;
}
else if (type == 'd') { //File creation
pfile->type = 'd';
printf("Enter file's text:\n");
scanf(" %s", &pfile->content.data);
}
return pfile;