#include #include #include #include struct cad_veiculo{ char modelo[80]; char placa[20]; char descricao[200]; int ano; }cad; //################### PROTOTIPOS && VARIABLES GLOBAIS ###################### void CADASTRAR(void); void PESQUISA(char pesq[200]); void MENU(void); void ALTERAR(void); //########################## FUNÇAO PRINCIPAL ################################ int main(){ textcolor(WHITE); system("MODE con cols=60 lines=30"); while(1){ MENU(); } } // ########################## FUNÇOES ############################################## void MENU(void){ int menu=0; char ch,pesq[200],excluir[200]; printf("\t\tMENU\n\n\n"); printf("1 - CADASTRAR \n\n"); printf("2 - PESQUISAR STRING\n\n"); printf("3 - LOCALIZAR/EDITAR \n\n"); printf("4 - SAIR\n\n"); printf("\n\n############################################\n\n\n"); scanf("%d",&menu); switch(menu) { case 1: system("cls"); CADASTRAR(); break; case 2: system("cls"); printf("Digite o que quer procurar -> "); fflush(stdin); fgets(pesq,sizeof(pesq),stdin); PESQUISA(pesq); break; case 3: system("cls"); ALTERAR(); break; case 4: exit(EXIT_SUCCESS); break; default: system("cls"); printf("Desculpe op\x87\xc6o errada !"); ch=getche(); system("cls"); } } void ALTERAR(void){ char placa[20]; char opc; FILE *file; int regnum=0,pos=0; if((file=fopen("cad_veiculo.txt","r+b")) == NULL){ //se o arquivo nao existe mostra a mensagem abaixo e sai printf("\nERRO AO ABRIR O ARQUIVO ! "); for(int x=0;x<999999999;x++); //um time para dar tempo de ver a mensagem acima exit(EXIT_SUCCESS); } printf("\nDIGITE A PLACA -> "); fflush(stdin); fgets(placa,sizeof(placa),stdin); while(!feof(file)){ if(fread(&cad,sizeof(struct cad_veiculo),1,file)==1){ if(strcmp(placa,cad.placa)==0){ printf("Modelo :%s\n Placa :%s\n Ano :%d\n Descricao :%s\n",cad.modelo,cad.placa,cad.ano,cad.descricao); regnum = pos+1; break; } } pos++; } if(regnum==0){ printf("\nDESCULPE CADASTRO NAO LOCALIZADO!\n\n\n"); }else{ printf("\nDESEJA ALTERAR O REGISTRO?(s/n)"); scanf("%c",&opc); getchar(); if(opc=='s' || opc=='S'){ printf("\nATUALIZAR CADASTRO!"); printf("\n\nModelo : "); fflush(stdin); fgets(cad.modelo,sizeof(cad.modelo),stdin); printf("\nPlaca : "); fflush(stdin); fgets(cad.placa,sizeof(cad.placa),stdin); printf("\nAno : "); fflush(stdin); scanf("%d",&cad.ano); printf("\nDescricao : "); fflush(stdin); fgets(cad.descricao,sizeof(cad.descricao),stdin); printf("\n\n"); getchar(); fseek(file,sizeof(struct cad_veiculo)*(regnum-1),SEEK_SET); fwrite(&cad,sizeof(struct cad_veiculo),1,file); printf("\t*******ARQUIVO ALTERADO COM SUCESSO******\n\n"); pos=0; regnum=0; } } fclose(file); } void PESQUISA(char pesq[200]){ FILE *file; char temp[200]; if((file=fopen("cad_veiculo.txt","r")) == NULL){ //se o arquivo nao existe mostra a mensagem abaixo e sai printf("\nERRO AO ABRIR O ARQUIVO ! "); for(int x=0;x<999999999;x++); //um time para dar tempo de ver a mensagem acima exit(EXIT_SUCCESS); } while(fgets(temp,200,file)!=NULL){ if(strstr(temp,pesq)){ textcolor(RED); printf("\n\n%s",strupr(temp)); textcolor(WHITE); printf("\n\n\n"); } } fclose(file); } //CADASTRA VEICULO void CADASTRAR(void){ #define ESC 27 int year,key=0; FILE *cad_veiculo; cad_veiculo=fopen("cad_veiculo.txt","a"); do{ printf("\t\tCADASTRO\n\n\n"); printf("\t\tPRESSIONE ESC PARA SAIR\n\n\n"); printf("\nModelo : "); fflush(stdin);//limpando o buffer fgets(cad.modelo,sizeof(cad.modelo),stdin); printf("\nAno : "); fflush(stdin); scanf("%d",&year); printf("\nPlaca : "); fflush(stdin); fgets(cad.placa,sizeof(cad.placa),stdin); printf("\nDescricao : "); fflush(stdin); fgets(cad.descricao,sizeof(cad.descricao),stdin); cad.ano=year; //gravando no arquivo fprintf(cad_veiculo,"\nPlaca : %s",cad.placa); fprintf(cad_veiculo,"\nModelo : %s" ,cad.modelo); fprintf(cad_veiculo,"\nAno : %d",cad.ano); fprintf(cad_veiculo,"\nDescricao : %s",cad.descricao); fprintf(cad_veiculo,"\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); fflush(stdin); printf("\n\nDESEJA CONTINUAR ?\n\n"); printf("DIGITE QUALQUER TECLA PARA CONTINUAR OU ESC PARA SAIR\n\n"); key=getch(); }while(key!=27); //se precionar ESC sai do programa system("cls"); }