Save/Read large files

Asked

Viewed 86 times

1

I’m having some difficulty in an academic project where I have to generate a certain number of records to have an X-size file. Ex.: 20gb of one .bin for so many records.

My problem is how to do this as soon as a file of this bursts the memory of any basic micro.

My project is currently like this:

List. c

#include "Lista.h"
#include <stdlib.h>
#include <stdio.h>

void inicializa_lista(Lista *l, int t)
{
    l->tamInfo = t;
    l->cabeca = NULL;
}

int insereNoInicio(Lista *l, void *info)
{
    Elemento *p = aloca_elemento(l->tamInfo, info);

    if(p == NULL)
        return 0;/*Erro na alocação.*/

    p->info = malloc(l->tamInfo);

    if(p->info == NULL)
    {
        free(p);
        return 0;/*Erro.*/
    }

    memcpy(p->info, info, l->tamInfo);

    p->proximo = l->cabeca;

    l->cabeca = p;

    return 1;
}

int insereNoFim(Lista *l, void *info)
{
    if(lista_vazia(*l))
        return insereNoInicio(l, info);

    Elemento *p = aloca_elemento(l->tamInfo, info);

    if(p == NULL)
        return 0;

    Elemento *aux = l->cabeca;

    while(aux->proximo != NULL)
        aux = aux->proximo;

    p->proximo = NULL;

    aux->proximo = p;

    return 1;
}

int removeNoInicio(Lista *l, void *info)
{
    if(lista_vazia(*l))
        return ERRO_LISTA_VAAZIA;

    Elemento *p = l->cabeca;

    l->cabeca = p->proximo;/*equivalentes l->cabeca = l->cabeca->proximo;*/

    memcpy(info, p->info, l->tamInfo);

    free(p->info);

    free(p);

    return 1;
}

int removeNoFim(Lista *l, void *info)
{
    if(lista_vazia(*l))
        return ERRO_LISTA_VAAZIA;

    if(l->cabeca->proximo == NULL)/* somente quando a lista tem um elemento */
        return removeNoInicio(l, info);

    Elemento *p = l->cabeca;

    while(p->proximo->proximo != NULL)
        p = p->proximo;

    memcpy(info, p->proximo->info, l->tamInfo);

    free(p->proximo->info);
    free(p->proximo);

    p->proximo = NULL;

    return 1;
}

int lista_vazia(Lista l)
{
    return l.cabeca == NULL;
}

Elemento *aloca_elemento(int tamInfo, void *info)
{
    Elemento *p = malloc(sizeof(Elemento));

    if(p == NULL)
        return NULL;

    p->info = malloc(tamInfo);

    if(p->info == NULL)
    {
        free(p);

        return NULL;
    }

    memcpy(p->info, info, tamInfo);

    return p;
}

void mostra_lista(Lista l, void (*mostra_info)(void *))
{
    if(lista_vazia(l))
        printf("A lista está vazia\n");
    else
    {
        Elemento *p = l.cabeca;

        printf("Dados da Lista:\n");

        while(p != NULL)
        {
            mostra_info(p->info);
            p = p->proximo;
        }
    }
}

void limpa_lista(Lista *l)
{
    Elemento *p = l->cabeca;

    while(p != NULL)
    {
        Elemento *aux = p->proximo;

        free(p->info);
        free(p);

        p = aux;
    }

    l->cabeca = NULL;
}

int insereNaPosicao(Lista *l,void *info,int pos){
    if(pos<0)
        return ERRO_POSICAO_INVALIDA;
    if(pos==0)
        return insereNoFim(l,info);
    Elemento *p=l->cabeca;
    int cont =0;
    while(cont<pos-1 && p->proximo!=NULL){
        p=p->proximo;
        cont++;
    }
    if(cont!=pos-1)
        return ERRO_POSICAO_INVALIDA;
    Elemento *novo = aloca_elemento(l->tamInfo,info);
    if(novo==NULL)
        return 0; // ERRO ALOCACAO
    novo->proximo=p->proximo;
    p->proximo=novo;
    return 1;
}

int removeNaPosicao(Lista *l,void *info,int pos){
    if(lista_vazia(*l)) return ERRO_LISTA_VAAZIA;
    if(pos<0) return ERRO_POSICAO_INVALIDA;
    Elemento *p = l->cabeca;
    if(pos==0){
        removeNoInicio(l,info);
    }
    int cont;
    while(cont<pos-1 &&p->proximo!=NULL){
        p=p->proximo;
        cont++;
    }
    if(cont!=pos-1) return ERRO_POSICAO_INVALIDA;
    Elemento *aux = p->proximo;
    p->proximo = aux ->proximo;
    free(aux->info);
    free(aux);
    return 1;
}


int compara_float(void *a,void *b){
    float *p1=a,*p2=b;
    if(*p1>*p2) return 1;
    if(*p1<*p2) return -1;
    return 0;
}


int insereEmOrdem(Lista *l,void *info,int(*compara)(void*,void*)){
    int cont =0;
    Elemento *p = l->cabeca;
    while(p!=NULL && compara(info,p->info)>0){
        cont++;
        p=p->proximo;
    }
    return insereNaPosicao(l,info,cont);

}

The structs I’m using:

typedef struct{
    char nome[24];
    int matricula;
    int notas;
    int faltas;
}Diario;

typedef struct ele
{
    void *info;
    struct ele *proximo;
}Elemento;

typedef struct
{
    int tamInfo;
    Elemento *cabeca;
}Lista;

How am I using a chained list where at each node I allocate one struct Diario at random values.

inicializa_lista(&l1, sizeof(Diario)); // Inicializo a lista, com o tamanho do dado a ser inserido.

insereNoInicio(&l1,&aux); // Insiro um elemento no qual &aux é o endereço de um elemento Diario.

my problem is going through the list and writing them to a file as soon as I use this function for the same:

void mostra_registro(Diario *p){
    FILE *fp;
    fp = fopen("vaicaber.bin","a+b");
    fwrite(p,sizeof(Diario), sizeof(Diario)*1024,fp);
    fclose(fp);
    // printf("%d %22s\n", p->matricula, p->nome, p->matricula);
}
  • 5

    File does not burst memory. What bursts memory is to load everything at once into it.

  • 4

    It is generating the records to popular the file with the 20GB, oh yes, if you decide to record everything at once in memory to only then give the flush to the file for sure your memory will pro clay. Try to do this process in parts by saving the contents to each X records.

  • @user54408: Let me see if I understand, do you want to load a 2GB binary file into memory to then sort it ? Or you want to write 2GB of memory to file ?

  • @user54408: https://en.wikipedia.org/wiki/External_sorting

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.