Advertisement
Guest User

Untitled

a guest
May 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. int introduzCategorias(int iCategoria, CATEGORIA array[]){
  2.     CATEGORIA aux;
  3.     int existe = 0;
  4.    
  5.     FILE *fp;
  6.     fp=fopen("categorias.dat","ab");
  7.    
  8.     if(fp == NULL){
  9.         fclose(fp);
  10.         fp=fopen("categorias.dat","wb");
  11.        
  12.        
  13.     }
  14.     aux.ID = iCategoria;
  15.    
  16.     printf("Introduza uma categoria: \n");
  17.     gets(aux.nome);
  18.     for (int i = 0; i<iCategoria; i++){
  19.         if(strcmp(aux.nome, array[i].nome) == 0){
  20.             existe = 1;
  21.    
  22.         }
  23.     }
  24.     if (existe != 1){
  25.         aux.ID = iCategoria;
  26.         array[iCategoria] = aux;
  27.         fwrite(&aux, sizeof(CATEGORIA),1, fp );
  28.         fclose(fp);   //fechar apontador
  29.         return 1;
  30.     }
  31.     else {
  32.         system("clear");
  33.         printf("Essa categoria já existe \n");
  34.         getchar();
  35.         return 0;
  36.     }
  37. }
  38.  
  39. void listarCategorias(int iCategoria, CATEGORIA array[]){
  40.    
  41.     int i;
  42.     for(i=0;i<iCategoria;i++){
  43.        
  44.         printf("%i - %s\n",array[i].ID+1, array[i].nome);
  45.     }
  46.    
  47.     printf(">> 0 - Voltar << \n");
  48.     getchar();
  49.    
  50. }
  51.  
  52. int carregarCategorias(CATEGORIA array[]){
  53.     FILE *fp;
  54.     CATEGORIA cat;
  55.     fp=fopen("categorias.dat","rb");
  56.     if(fp == NULL){
  57.         return 0;
  58.     }
  59.     int i = 0;
  60.     while(fread(&cat,sizeof(CATEGORIA),1,fp) == 1){
  61.         array[i] = cat;
  62.         i++;
  63.     }
  64.     return i;
  65. }
  66.  
  67. void alterarCategorias(int iCategoria, CATEGORIA array[]){
  68.     int i, op = 0;
  69.     int id = 0;
  70.     int existe = 0;
  71.     char str[50];
  72.     char nomeTemp[100]={0};
  73.     CATEGORIA aux;
  74.    
  75.    
  76.     for(i=0;i<iCategoria;i++){
  77.        
  78.         printf("%i - %s\n",array[i].ID+1, array[i].nome);
  79.     }
  80.    
  81.     FILE *fp;
  82.     fp=fopen("categorias.dat","rb+");
  83.    
  84.     if(fp == NULL){
  85.         fclose(fp);
  86.         printf("Erro ao abrir o ficheiro \n");
  87.         return;
  88.        
  89.        
  90.     }
  91.    
  92.    
  93.    
  94.     printf("Escolha a categoria que pretende alterar: \n");
  95.     scanf("%i", &op);
  96.    
  97.     getchar();
  98.     for(i = 0; i<iCategoria; i++){
  99.         if(op == array[i].ID+1){
  100.             system("clear");
  101.             printf("Altere a categoria %s para o que desejar: \n", array[i].nome);
  102.             gets(nomeTemp);
  103.             strcpy(str,array[i].nome);
  104.             id = array[i].ID;
  105.            
  106.             for( int j = 0; j<iCategoria; j++){
  107.                 if(strcmp(nomeTemp, array[j].nome) == 0){
  108.                     existe = 1;
  109.                     system("clear");
  110.                     printf("A categoria que introduziu já existe \n");
  111.                 }
  112.            
  113.             }
  114.            
  115.            
  116.            
  117.            
  118.             while(fread(&aux,sizeof(CATEGORIA),1,fp) == 1){
  119.                 if(strcmp(aux.nome,str)== 0){
  120.                     fseek(fp,-1*sizeof(CATEGORIA), SEEK_CUR);
  121.                     strcpy(aux.nome, nomeTemp);
  122.                     aux.ID == id;
  123.                     fwrite(&aux, sizeof(CATEGORIA), 1, fp);
  124.                     strcpy(array[i].nome,nomeTemp);
  125.                 }
  126.                
  127.             }
  128.             fclose(fp);
  129.            
  130.             if(existe == 0){
  131.                 system("clear");
  132.                 printf("Categoria alterada com sucesso \n");
  133.                
  134.            
  135.             }
  136.    
  137.         }
  138.         else{
  139.             printf("O número que introduziu não exite \n");
  140.        
  141.         }
  142.          
  143.        
  144.        
  145.     }
  146.    
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement