Delete record in text file

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

I am having great difficulty in designing the functions of delete e modify movies and series.

First, it follows the definition of the structures of films and Series:

#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);

As the file movies.c is essentially the same as series.c , follows only this one for follow-up effect:

#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, follow the main program 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 some of the de facto code of these two functions in question, it would be of immense help!

 0
Author: Victor Stafusa, 2017-07-03

1 answers

  • Comment *

Man, because you're working with Vector a logic has to be something like this:

Modify: 1-Ask what the name of the film. 2-scan the vector by comparing. 3 - with the vector in hand, passes the insertion function but with the content of the found.

Delete: Same scan logic to find, but when you find you can put the last movie in that position, if the order of the movies does not import.

I STRONGLY RECOMMEND USING LINKED LIST INSTEAD OF VECTOR!

 0
Author: M. Bertolazo, 2017-07-04 14:13:06