要填充元数据的结构 [英] Want to fill Structure for Metadata

查看:52
本文介绍了要填充元数据的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个目录,其中包含许多文件.

Here i have one directory which has number of files.

我想将所有信息填充到一个结构中.

I want to fill this files all information in one structure.

我有以下两个结构.

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;

我想制作一个标头,其中包含有关这些文件的所有信息.

i want to make one header which contains all information regarding these files.

就像我有3个文件而不是要在file_count = 3中使这种结构一样,我该如何分配第二个变量值?并希望按文件存储文件名和文件大小.

like if i have 3 files than i want to make this structure like this in file_count = 3 and how can i allocate second variable value? and want to store file name and file size as per file.

我想要这样的文件结构

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

我对文件名和文件大小具有所有逻辑,但是如何在此结构中填充这些内容??

I have all logic about file name and file size but how can i fill these things in this structure.?

代码:

#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

它显示不同的输出.那么这里出什么问题了?

Its shows different output. so whats problem here?

推荐答案

在其他事情中,您正在对指针变量使用sizeof,但似乎认为这可以使您指向的对象大小.没有.为此,请使用星号运算符使表达式具有指针指向的类型:

Among other things, you are using sizeof on a pointer variable, but seem to think that gives you the size of the object being pointed to. It doesn't. To do that, use the asterisk operator to make the expression have the type that the pointer points at:

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

请注意,sizeof不是函数,因此不需要括号.当您看到括号时,即表示它们是表达式(强制转换)的一部分.

As a side note, notice that sizeof is not a function, so you don't need parenthesis. When you do see parenthesis, that's when they're part of the expression (a cast).

这篇关于要填充元数据的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆