Delete record in text file

Asked

Viewed 681 times

0

I am making a program that consists of recording movies/series, modifying information and deleting specified records.

I’m having great difficulty in designing the functions of rule out and modify films and series.

First, it follows the definition of film and series structures:

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

//Estruturas principais

typedef struct filmes
{
    char nome[50];
    char diretor[30];
    char elenco[80];
    int ano;
    char genero[20];

}Filmes;

typedef struct series
{
    char nome[50];
    char criador[30];
    char elenco[80];
    int ano;
    int numeroTemporadas;
    char genero[20];

}Series;

//Cabeçalho das funções de funções.h

//FILMES
void adicionarFilme(Filmes *filmes, int *posF, int *nFilmes, FILE *arq);
void modificarFilme();
void removerFilme();
void imprimeFilmes(Filmes *filmes, int *nFilmes);
void managementFilmes(int codigo, Filmes *filmes, int *posF, int *nFilmes, FILE *arq);
int buscaFilme(FILE *arq, Filmes filmes);

//SÉRIES
void adicionarSerie(Series *series, int *posS, int *nSeries, FILE *arq);
void modificarSerie();
void removerSerie();
void imprimeSeries(Series *series, int *nSeries);
void managementSeries(int codigo, Series *series, int *posS, int *nSeries, FILE *arq);

Like the file films. c is essentially the same as series. c, follow only this for monitoring:

#include<stdio.h>
#include<stdlib.h>
#include "estruturas.h"
#include<string.h>

void adicionarFilme(Filmes *filmes, int *posF, int *nFilmes, FILE *arq)
{
    *nFilmes = *nFilmes + 1;

    filmes = (Filmes *) realloc(filmes, *nFilmes * sizeof(Filmes));

    if(!filmes)
    {
        exit(1);
    }

    printf("\n\n\tNome: ");
    scanf(" %[^\n]s", filmes[*posF].nome);
    printf("\n\n\tDiretor: ");
    scanf(" %[^\n]s", filmes[*posF].diretor);
    printf("\n\n\tElenco: ");
    scanf(" %[^\n]s", filmes[*posF].elenco);
    printf("\n\n\tAno: ");
    scanf("%d", &filmes[*posF].ano);
    printf("\n\n\tGenero: ");
    scanf(" %[^\n]s", filmes[*posF].genero);

    fprintf(arq, "\n\tFilme: %s", filmes[*posF].nome);
    fprintf(arq, "\n\tDiretor: %s", filmes[*posF].diretor);
    fprintf(arq, "\n\tElenco: %s", filmes[*posF].elenco);
    fprintf(arq, "\n\tAno: %d", filmes[*posF].ano);
    fprintf(arq, "\n\tGenero: %s\n\n", filmes[*posF].genero);

    *posF = *posF + 1; //incrementa posição do vetor
}

void modificarFilme()
{
        //como faço essa função?
}

void removerFilme()
{
        //como faço essa função?
}

void imprimeFilmes(Filmes *filmes, int *nFilmes)
{
    int i;
    int x = 1;

    printf("\n\tFILMES\n");
    for(i = 0; i < *nFilmes ; i++)
    {
        printf("\n\tNOME:\t%s", filmes[i].nome);
        printf("\n\tDIRETOR:\t%s", filmes[i].diretor);
        printf("\n\tELENCO:\t%s", filmes[i].elenco);
        printf("\n\tANO:\t%d", filmes[i].ano);
        printf("\n\tGENERO:\t%s", filmes[i].genero);

        printf("\n\n");
        x++;
    }
}

void managementFilmes(int codigo, Filmes *filmes, int *posF, int *nFilmes, FILE *arq)
{
    if(codigo == 1)
    {
        adicionarFilme(filmes, posF, nFilmes, arq);
    }
    if(codigo == 2)
    {
        //função de remover/excluir
    }
    if(codigo == 3)
    {
        //função de modificar
    }
    if(codigo == 4)
    {
        imprimeFilmes(filmes, nFilmes);
    }
    if(codigo == 0)
    {
        return;
    }
}

Records are made in a file txt !

Finally, it follows the main programme for reference:

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

void criar(FILE *arq);

int main()
{   
    Filmes *filmes;
    Series *series;

    int posF = 0;
    int posS = 0;

    int numeroFilmes = 0;
    int numeroSeries = 0;

    char codA;
    int codB;

    FILE *arq;  

    do{
        /**********************************
            Código A:

            F - Filmes
            S - Series
            X - Encerra
        ***********************************/
        printf("\n\tCOMANDOS: ");
        printf("\n\n\tF - Filmes\n\n\tS - Series\n\n\tX - Encerra\n\n\t");
        scanf("%c", &codA);

        fflush(stdin);

        if(codA == 'F' || codA == 'f')
        {
            /**********************************
                Código B:

                1 - Adicionar filme
                2 - Remover filme
                3 - Modificar informações de um filme
                4 - Imprime registro de filmes
                0 - Sair
            ***********************************/
            printf("\n\tCOMANDOS: ");
            printf("\n\n\t1 - Adicionar filme\n\n\t2 - Remover filme\n\n\t3 - Modificar informacoes de um filme\n\n\t4 - Imprime registro de filmes\n\n\t0 - Sair\n\n\t");
            scanf("%d", &codB);

            arq = fopen("filmes.txt", "a++");
            if(!arq)
            {
                printf("\n\n\tArquivo nao pode ser aberto corretamente!");
                exit(1);
            }

            managementFilmes(codB, filmes, &posF, &numeroFilmes, arq);
            fflush(stdin);
            fclose(arq);
        }


        else if(codA == 'S' || codA == 's')
        {
            /**********************************
                Código B:

                1 - Adicionar série
                2 - Remover série
                3 - Modificar informações de uma série
                4 - Imprime registro de séries
                0 - Sair
            ***********************************/
            printf("\n\tCOMANDOS: ");
            printf("\n\n\t1 - Adicionar serie\n\n\t2 - Remover serie\n\n\t3 - Modificar informacoes de uma serie\n\n\t4 - Imprime registro de series\n\n\t0 - Sair\n\n\t");
            scanf("%d", &codB);

            arq = fopen("series.txt", "a++");
            if(!arq)
            {
                printf("\n\n\tArquivo nao pode ser aberto corretamente!");
                exit(1);
            }


            if(codB == 1 && isFirstTimeS == 0)
            {
                series = (Series *) malloc(sizeof(Series));
                isFirstTimeS = 1;
            }

            managementSeries(codB, series, &posS, &numeroSeries, arq);
            fflush(stdin);
            fclose(arq);
        }

        else if(codA == 'x' || codA == 'X')
        {
            break;
        }

        else
            printf("\n\n\tCodigo invalido!");

        fflush(stdin);

    }while(codA != 'X' || codA != 'x');

    if(numeroFilmes > 0)
        free(filmes);
    if(numeroSeries > 0)    
        free(series);

    return 0;
}

If you could help me with at least part of the actual code of these two functions in question, it would be of immense help!

  • Is it really necessary to do this way? It would be better to use a database.

  • In that case, yes...

  • As you are working with sequential file (txt) then to physically remove a particular record you have to create a new file and copy to this newly created file all the records that must remain. At the end delete the old file and rename the newly created file by overwriting the old deleted file.

  • Hi @Luigiwagner, why not use chained list? Wouldn’t it be easier and better to implement? Your txt is CSV standard?

1 answer

0

  • comment *

Dude, because you’re working with Vector logic it has to be something like this:

Modify: 1- Ask what the name of the film is. 2- Scan the vector by comparing. 3- With the vector in hand, pass the insertion function but with the contents of the found.

Exclude: Same scanning logic to find, but when you find you can put the last movie in this position, in case the order of the movies doesn’t matter.

STRONGLY RECOMMEND USING LINKED LIST INSTEAD OF VECTOR!

  • Old, the point is that the vector only has the records of each execution. Once an execution is terminated, records only remain recorded in the archive. I believe that the solution would involve going through the file itself - which is what, in this case, I don’t know how to do. Researching found that the way it involves the use of fseek and fread functions, both of which I’m having difficulty applying in this context.

  • So, man, I suggest you do it like this: create a txt pull function and put it on a Linked list and another pull on the Linked list pro txt.. will have to study the file library and the algorithm of Linked list.. by my experience is the best to do.

Browser other questions tagged

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