2012-03-12 121 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

这里的问题具体是什么? – 2012-03-12 11:11:10

+1

可能重复的[Create Customize Header(metadata)for files](http://stackoverflow.com/questions/9664536/create-customize-headermetadata-for-files) – 2012-03-12 11:15:59

+0

我可以通过链表实现这件事吗? – user1089679 2012-03-12 11:26:29

回答

0

别的不说,你在一个指针变量使用sizeof,但似乎认为给你的对象的大小被指出至。它没有。要做到这一点,使用星号操作人员进行表达上有以指针所指向的类型:

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

作为一个侧面说明,注意sizeof不是一个函数,所以你不需要括号。当你看到括号时,那就是他们是表达式的一部分(演员)。

+0

好的谢谢你的回复。但我不能能为这个做的代码,这样如何能实现这件事情? – user1089679 2012-03-12 12:02:22

0

你没有留下足够的空间,空终止:

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

问题是有我同意,但是我想使填充头这样并希望在文件中写入和 – user1089679 2012-03-12 12:03:01

+0

@ user1089679回读,以及:您需要先解决所有的错误。 – 2012-03-12 12:04:57