2012-03-12 7 views
0

여기에 파일 수가 많은 하나의 디렉토리가 있습니다.메타 데이터 구조체

이 정보를 하나의 구조에 모두 채우고 싶습니다.

다음 두 가지 구조가 있습니다.

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 

그것의 다른 출력을 보여줍니다. 여기 뭐가 문제 야?

+2

구체적으로 무엇이 문제입니까? –

+1

가능한 [Create Customize Header (metadata for) files] (http://stackoverflow.com/questions/9664536/create-customize-headermetadata-for-files) –

+0

링크 된 목록으로이 작업을 수행 할 수 있습니까? – user1089679

답변

0

을, 당신은 포인터 변수에 sizeof를 사용하지만이 지적되고 당신에게 개체의 크기를 제공합니다 생각하는 것있다 에. 그렇지 않습니다. 보조 노트로

printf("size of Header is %d\n", sizeof *metadata); 

, 당신은 괄호를 필요가 없습니다 sizeof 함수가 아닌 것을 알 수 :이를 위해 식을 만들기 위해 별표 (*) 연산자를 사용에서 포인터 포인트가되는 유형이있다. 괄호가 보이면 표현의 일부가됩니다 (캐스트).

+0

답장을 보내 주셔서 감사합니다. 하지만이 일을 성취 할 수있는 방법으로 코드를 수행 할 수 있습니까? – user1089679

0

당신은 널 (NULL) 종료를위한 충분한 공간을 남겨되지 않은 : 무엇보다도

header->file[file_count].file_name = (char *)malloc(sizeof(char)*strlen(entry->d_name)); 
+0

문제는 거기에 동의하지만 필자는 헤더를 채우고 파일에 쓰고 싶고 다시 읽으려고합니다. – user1089679

+0

@ user1089679 : 모든 버그를 먼저 고쳐야합니다. –