/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include #include #include typedef struct{ int id; char nome[100]; int idade; float salario; }dados; int id=4; void cadastro_dados(char *); void remove_dados(char *); int menu(); int main() { int op; // cadastro_dados("cadastro.txt"); op=menu(); while(op){ switch (op){ case 0: printf("Tchau!!!"); break; case 1: cadastro_dados("cadastro.txt"); break; // case 2: retorna_dados(); break; case 3: remove_dados("cadastro.txt"); break; /* case 4: altera_dados(); break;*/ default: printf("Opção Inválida"); } op=menu(); } return 0; } int menu(){ int op; printf("\n0-Sair\n1-Insere dados\n2-Retorna dados"); printf("\n3-Remove dados\n4-Altera dados\nOpção:"); scanf("%d",&op); return op; } void cadastro_dados(char *str){ FILE *arq; int sair=1; dados usr; arq=fopen(str,"a+"); if(arq==NULL){ printf("\nErro cadastro_dados"); exit(0); } do{ printf("Nome:"); scanf(" %[^\n]s",usr.nome); printf("Idade:"); scanf(" %d",&usr.idade); printf("Salario:"); scanf(" %f",&usr.salario); fprintf(arq,"%d %s\n",++id,usr.nome); fprintf(arq,"%d %.2f\n",usr.idade,usr.salario); printf("Digite 0 para sair:"); scanf("%d",&sair); }while(sair); fclose(arq); } void remove_dados(char *str){ FILE *arq = fopen(str,"r+"); dados usr; int id, aux=-1;; long int tam; printf("Id a ser removido:");scanf("%d",&id); while(fscanf(arq,"%d %[^\n]s\n",&usr.id,usr.nome)!=EOF){ if(usr.id==id){ tam = strlen(usr.nome)+2; fseek(arq,aux*tam,SEEK_CUR); fprintf(arq,"%d %s\n",aux,usr.nome); fseek(arq, 0, SEEK_END); continue; } fscanf(arq,"%d %f",&usr.idade,&usr.salario); } fclose(arq); }