从文件中读取C中的所有元素 [英] Reading from a file all elements within it in C

查看:83
本文介绍了从文件中读取C中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要编写一个函数来读取位文件中的所有元素.关键是我不知道内部可能有多少个元素,但我知道元素的类型.因此,我尝试编写此函数:

So I need to write a function that reads all the elements inside a bit file. The point is that I don't know how many elements there could be inside, but I know what type of elements are. So I tried to write this function:

   void loadData(Parallelogram **array) {
            FILE *data; long size;
            //int numberOfElements = 0;
            int numberOfObjects = 0;


            if ((data = fopen(name, "rb"))!=NULL) {


                fseek(data, 0, SEEK_END);
                size = ftell(data);
                fseek(data, 0, SEEK_SET);


                if (size<(long)sizeof(Parallelogram)) {

                    printf("The file is empty try to open another file maybe");

                } else {

                    Parallelogram *tempArray;

                    numberOfObjects = size/sizeof(Parallelogram);

                    tempArray = realloc(*array, numberOfObjects*sizeof(Parallelogram));

                    if (tempArray==NULL) {
                         printf("There was an error reallocating memory");
                    } else { *array = tempArray; }

                    fread(*array, sizeof(Parallelogram), numberOfObjects, data);

                }
            }
            fclose(data);
        }

元素是Parallelogram类型的结构对象,存储一些浮点数. 被注释掉的部分是我试图从另一个问题中尝试另一种方法,但不了解真正的机制.无论如何,当我调用函数时,数组为空.我怎么了?

The elements are struct objects of type Parallelogram, storing a few floats. The commented out part was me trying another method form another question but not understanding the real mechanism. Anyways when I call the function the array is empty. What am I getting wrong?

根据要求,这是主要函数,我在其中调用函数loadData()

As requested this is the main function where I call the function loadData()

int main() {
    Parallelogram *paraArray = NULL;
    loadData(&paraArray);
}

推荐答案

编辑:完成功能或多或少像OP一样.

complete function more or less like the OP's.

您可以执行以下操作:

void loadData(Parallelogram **array, size_t * n) {
    FILE *data;

    if ((data = fopen("file.bin", "rb"))!=NULL) {
        Parallelogram buffer[100]; // may be malloc'd
        size_t chunk_size = 100;
        size_t read_size = 0;
        size_t number_of_objects = 0;
        Parallelogram *aux = NULL;
        *array = NULL;

        while ((read_size = fread(buffer, sizeof *buffer, chunk_size, data)) > 0) {
            aux = realloc(*array, (number_of_objects + read_size) * sizeof *buffer);
            if (aux == NULL) {
                // ERROR
                free(*array);
                // clean, break/exit
            }
            *array = aux;
            memcpy(*array + number_of_objects, buffer, read_size*sizeof *buffer);
            number_of_objects += read_size;
        }
        // check file for errors (ferror()) before exit
        fclose(data);
        *n = number_of_objects;
    }
}

这篇关于从文件中读取C中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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