Write to information file in C

0

So, folks, I'm doing a job that essentially consists of recording information about movies and series in two files (one for each one).

In the code section movies.c I have all the functions used to manipulate information about movies (which is a vector that is dynamically allocated and reallocated), which has the functions ADD, REMOVE and MODIFY movies (the same exists in the series.c section section for the same series operations).

Follow the code for filmes.c below:

 void adicionarFilme(Filmes *filmes, int *posF, int *nFilmes)
 {
      *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);

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

 void modificarFilme(Filmes *filmes, int *nFilmes)
 {
      char nomeMod[50];
      int i, j, k;
      int tamString;
      int op;
      int segue = 1;

      printf("\n\n\t Insira o nome do filme a ser modificado: ");
      scanf(" %[^\n]s", nomeMod);

      tamString = strlen(nomeMod);

      for(i = 0; i < *nFilmes; i++)
      {
          k = 0;
          for(j = 0; j < tamString; j++)
          {
              if(filmes[i].nome[j] != nomeMod[k])
              {
                  segue = -1;
                  printf("\n\n\tFILME NAO ENCONTRADO!\n\n");
                  return;
              }
              k++;
          }
          if(segue == 1)
          {
              printf("\n\t1 - Modificar nome\n\t2 - Modificar diretor\n\t3 - Modificar elenco\n\t4 - Modificar ano\n\t5 - Modificar genero\n\n\t");
              scanf("%d", &op);

              switch(op)
              {
                  case 1: printf("\n\tInforme o novo nome: \n\n\t");
                          scanf(" %[^\n]s", filmes[i].nome);
                          break;
                  case 2: printf("\n\tInforme o novo diretor: \n\n\t");
                          scanf(" %[^\n]s", filmes[i].diretor);
                          break;
                  case 3: printf("\n\tInforme o novo elenco: \n\n\t");
                          scanf(" %[^\n]s", filmes[i].elenco);
                          break;
                  case 4: printf("\n\tInforme o novo ano: \n\n\t");
                          scanf(" %d", &filmes[i].ano);
                          break;
                  case 5: printf("\n\tInforme o novo genero: \n\n\t");
                          scanf(" %[^\n]s", filmes[i].genero);
                          break;                
            }
            return;
         } 
     }
 }

 void removerFilme(Filmes *filmes, int *nFilmes)
 {
     char nomeDel[50];
     int i, j, k;
     int tamString;
     int op;
     int segue = 1;
     Filmes aux;

     printf("\n\n\t Insira o nome do filme a ser removido do catalogo: ");
     scanf(" %[^\n]s", nomeDel);

     tamString = strlen(nomeDel);

     for(i = 0; i < *nFilmes; i++)
     {
         k = 0;
         for(j = 0; j < tamString; j++)
         {
             if(filmes[i].nome[j] != nomeDel[k])
             {
                segue = -1;
                printf("\n\n\tFILME NAO ENCONTRADO!\n\n");
                return;
             }
             k++;
         }      
         if(segue == 1)
         {
             filmes[i].ano = -1; //para verificar. Se ano for -1, filme será removido

             for(j = 0; j < *nFilmes; j++)
             {
                 if(filmes[j].ano == -1)
                 {
                    aux = filmes[j];
                    filmes[j] = filmes[j+1];
                    filmes[j+1] = aux;
                 }
             }
         }
      }

      *nFilmes = *nFilmes - 1; //decrementa numero de filmes

      if(*nFilmes == 0)
      {
         return;
      }

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

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

 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)
 {
     if(codigo == 1)
     {
         adicionarFilme(filmes, posF, nFilmes);
     }
     if(codigo == 2)
     { 
         removerFilme(filmes, nFilmes);
     }
     if(codigo == 3)
     {
         modificarFilme(filmes, nFilmes);
     }
     if(codigo == 4)
     {
         imprimeFilmes(filmes, nFilmes);
     } 
     if(codigo == 0)
     {
         exit(1);
     }
}

The code for series.c is basically the same, so I do not think there is a need to link.

> Well what I would like for your help is this:

- I need all movies that are inserted, to be written to a file (preferably .txt), one below the other (like a list). - Every time the program is run I need the movies to be added to be added right after the last copy of the list.

I imagine the solution to this problem is not very complicated, but I'm really kind of lost on how to do it.

Help with your own code for manipulating file information would be greatly appreciated.

Finally, for reference effect, follows the code of the main program (main.c).

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

int posF = 0;
int posS = 0;

int numeroFilmes = 0;
int numeroSeries = 0;

int isFirstTimeF = 0;
int isFirstTimeS = 0;
char codA;
int codB;

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

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


        if(codB == 1 && isFirstTimeF == 0)
        {
            filmes = (Filmes *) malloc(sizeof(Filmes));
            isFirstTimeF = 1;
        }

        managementFilmes(codB, filmes, &posF, &numeroFilmes);
    }


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


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

        managementSeries(codB, series, &posS, &numeroSeries);
    }

}while(codA != 'X');

free(filmes);
free(series);

return 0;
} 

-------------------------------------------- -------------------------------------------------- ------------------------------------ Thanks for any help beforehand!

Edit: Definition of film structures 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(Filmes *filmes, int *nFilmes);
    void removerFilme(Filmes *filmes, int *nFilmes);
    void imprimeFilmes(Filmes *filmes, int *nFilmes);
    void managementFilmes(int codigo, Filmes *filmes, int *posF, int *nFilmes, FILE *arq);

    //SÉRIES
    void adicionarSerie(Series *series, int *posS, int *nSeries);
    void modificarSerie(Series *series, int *nSeries);
    void removerSerie(Series *series, int *nSeries);
    void imprimeSeries(Series *series, int *nSeries);
    void managementSeries(int codigo, Series *series, int *posS, int *nSeries);
    
asked by anonymous 30.06.2017 / 01:44

0 answers