Jump to content

Guardar, editar e carregar dados de um ficheiro


flOwy1233

Recommended Posts

Olá a todos. Foi-me pedido para desenvolver um código em que, uma das funcionalidades é carregar, editar e excluir dados de um ficheiro. O programa não paresenta erros de compilação, mas quando fecho e volto a abrir, os dados não são guardados no ficheiro.

Aqui estão as structs:
 

typedef enum {
    MICRO, PME, GRANDE, EMPTY
} Category;


typedef enum {
    INATIVO, ATIVO
} Status;

typedef struct {
    char branch[MAX_BRANCH];
} Branch;

typedef struct {
    int maxBranch;
    int branchCounter;
    Branch *branch;
    Status status;
} BranchActivity;

typedef struct {
    char title[MAX_TITLE];
    char text[MAX_TEXT];
    char username[MAX_USERNAME];
    char email[MAX_EMAIL];
    Status status;
} Comment;

typedef struct {
    int rating;
} Classification;

typedef struct {
    unsigned int nif;
    char name[MAX_NAME];
    Category companyCategory;
    char branch[MAX_BRANCH];
    char adress[MAX_ADRESS];
    char location[MAX_LOCATION];
    char postalCode [MAX_POSTAL_CODE];
    Status status;
    int maxComments;
    int commentsCounter;
    int maxClassification;
    int classificationCounter;
    Classification *classification;
    Comment *comments;
} Company;


typedef struct {
    int companiesCounter;
    int maxCompanies;
    Company *company;
} Companies;

As funções respetivas aos ficheiros: 
 

int getFileCounter(char *filename) {
    int counter = 0;
    FILE *fp = fopen(filename, "rb");
    if (fp != NULL) {
        fread(&counter, sizeof (int), 1, fp);
        fclose(fp);
    }
    return counter;
}

void updateFileCounter(int counter, char *filename) {
    FILE *fp = fopen(filename, "rb+");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }
    fwrite(&counter, sizeof (int), 1, fp);
    fclose(fp);
}

void loadCompaniesFromFile(Companies *companies, char *filename) {
    FILE *fp = fopen(filename, "rb");
    if (fp == NULL) {
        fp = fopen(filename, "wb");
    } else {
        fread(&companies->companiesCounter, sizeof (int), 1, fp);
        if (companies->companiesCounter > 0) {
            fread(companies->company, sizeof (Company), companies->companiesCounter, fp);
        }
    }

    fclose(fp);
}

void insertCompanyInFile(Companies companies, char *filename) {
    FILE *fp = fopen(filename, "ab");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }

    fwrite(&companies.company[companies.companiesCounter - 1], sizeof (Company), 1, fp);

    fclose(fp);
    
    updateFileCounter(companies.companiesCounter, filename);
}

void updateCompanyInFile(Companies companies, int index, char *filename) {
    FILE *fp = fopen(filename, "rb+");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }

    fseek(fp, sizeof (int), SEEK_SET);
    fseek(fp, sizeof (Company) * index, SEEK_CUR);

    fwrite(&companies.company[index], sizeof (Company), 1, fp);
}

void removeCompanyFromFile(Companies companies, char* filename) {
    FILE *fp = fopen(filename, "wb");
    if (fp == NULL) {
        exit(EXIT_FAILURE);
    }
    
    fwrite(&companies.companiesCounter, sizeof (int), 1, fp);
    fwrite(companies.company, sizeof (Company), companies.companiesCounter, fp);
}

Agradeço toda a ajuda possível 😀

It's dangerous to go alone! Take this.

Link to comment
Share on other sites

os métodos:

void removeCompanyFromFile(Companies companies, char* filename)
void updateCompanyInFile(Companies companies, int index, char *filename)

abrem o ficheiro mas não o fecham.
É necessário fechar o ficheiro com o fclose após qualquer operação, para guardar as alterações, libertar a memória e poder ser lido por outros programas.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.