여기에 파일 수가 많은 하나의 디렉토리가 있습니다.메타 데이터 구조체
이 정보를 하나의 구조에 모두 채우고 싶습니다.
다음 두 가지 구조가 있습니다.
struct files {
char *file_name;
int file_size;
};
typedef struct file_header {
int file_count;
struct files file[variable as per number of files];
} metadata;
이 파일과 관련된 모든 정보가 포함 된 하나의 헤더를 만들고 싶습니다.
내가 3보다 파일을 가지고있는 것보다이 구조를 이렇게 만들고 싶다면 file_count = 3
그리고 어떻게 두 번째 변수 값을 할당 할 수 있습니까? 파일 당 파일 이름 및 파일 크기를 저장하려고합니다.
내가 원하는 내가 파일 이름과 파일 크기 만에 대한 모든 논리를 가지고이
file_count = 3
file[0].file_name = "a.txt"
file[0].file_size = 1024
file[1].file_name = "b.txt"
file[1].file_size = 818
file[2].file_name = "c.txt"
file[2].file_size = 452
같은 파일 구조는 내가이 구조에서 이러한 것들을 채울 수있는 방법에 대해 설명합니다.?
코드 :
이#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
char path[1024] = "/home/test/main/Integration/testing/package_DIR";
//int count = 5;
struct files {
char *file_name;
int file_size;
};
typedef struct file_header {
int file_count;
struct files file[5];
} metadata;
metadata *create_header();
int main() {
FILE *file = fopen("/home/test/main/Integration/testing/file.txt", "w");
metadata *header;
header = create_header();
if(header != NULL)
{
printf("size of Header is %d\n",sizeof(metadata));
}
if (file != NULL) {
if (fwrite(&header, sizeof(metadata), 1, file) < 1) {
puts("short count on fwrite");
}
fclose(file);
}
file = fopen("/home/test/main/Integration/testing/file.txt", "rb");
if (file != NULL) {
metadata header = { 0 };
if (fread(&header, sizeof(header), 1, file) < 1) {
puts("short count on fread");
}
fclose(file);
printf("File Name = %s\n", header.file[0].file_name);
printf("File count = %d\n", header.file_count);
printf("File Size = %d\n", header.file[0].file_size);
}
return 0;
}
metadata *create_header()
{
int file_count = 0;
DIR * dirp;
struct dirent * entry;
dirp = opendir(path);
metadata *header = (metadata *)malloc(sizeof(metadata));
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
header->file[file_count].file_name = (char *)malloc(sizeof(char)*strlen(entry->d_name));
strcpy(header->file[file_count].file_name,entry->d_name);
//Put static but i have logic for this i will apply later.
header->file[file_count].file_size = 10;
file_count++;
}
}
header->file_count = file_count;
closedir(dirp);
//printf("File Count : %d\n", file_count);
return header;
}
출력 :
size of Header is 88
ile Name = �~8
File count = 29205120
File Size = -586425488
그것의 다른 출력을 보여줍니다. 여기 뭐가 문제 야?
구체적으로 무엇이 문제입니까? –
가능한 [Create Customize Header (metadata for) files] (http://stackoverflow.com/questions/9664536/create-customize-headermetadata-for-files) –
링크 된 목록으로이 작업을 수행 할 수 있습니까? – user1089679