Ir ao conteúdo
  • Cadastre-se

wendelroberta

Membro Júnior
  • Posts

    7
  • Cadastrado em

  • Última visita

Reputação

6
  1. editei algumas coisas, e percebi que o excluir e o editar parou de funcionar, alguma dica do que é um erro? to a mais de uma hora olhando e ja perquisei no google e ainda não faço ideia. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <errno.h> #define ARQ "Dados.dat" #define OP_INSERIR '1' #define OP_ALTERAR '2' #define OP_APAGAR '3' #define OP_LISTAR '4' #define OP_PESQUISAR '5' #define OP_ORDENAR '6' #define OP_SAIR '0' #define OP_PESQ_NOME '1' int variavelglobal = 0; char *MainMenu[]={ "1. Inserir contato", "2. Alterar contato", "3. Apagar contato", "4. Listar contatos", "5. Pesquisar", "6. Ordenar", "0. Sair", NULL }; char *PesqMenu[]={ "1. Pesquisar por nome", NULL }; FILE *fp; typedef struct{ char nome[30+1]; char numero[17]; int indice; char Status; }Pessoa; void Mensagem(char *msg); void Ler_Pessoa(Pessoa *p){ printf("Nome: "); gets(p->nome); printf("Numero: "); gets(p->numero); p->Status = ' '; fflush(stdin); } int i= 1; void Mostrar_Pessoa(Pessoa p){ printf("%d: %15s %4s\n", i, p.nome, p.numero); i+=1; } void Adiciona_Pessoa(Pessoa p){ fseek(fp, 0L, SEEK_END); if(fwrite(&p, sizeof(p), 1, fp)!=1) Mensagem("Adicionar pessoa: Falhou a escrita do contato"); } void Mensagem(char *msg){ printf(msg); getchar(); } void Inic(){ fp= fopen(ARQ, "r+b"); if(fp==NULL){ fp = fopen(ARQ, "w+b"); if(fp==NULL){ printf(stderr," Erro fatal: impossivel criar arquivo de dados\n"); exit(1); } } } char Menu(char *options[]){ int i; char ch; while(1){ printf("\n\n\n\n\n"); for(i=0; options[i]!=NULL; i++) printf("\t\t%s\n\n",options[i]); printf("\n\n\t\tOpcao: "); ch = getchar(); fflush(stdin); for(i=0; options[i]!= NULL; i++) if(options[i][0]==ch) return ch; } } void Inserir_Pessoa(){ Pessoa x; Ler_Pessoa(&x); x.indice = variavelglobal; variavelglobal++; Adiciona_Pessoa(x); } void Alterar_Pessoa(){ Pessoa x; long int nome; printf("Qual o nome do contato: "); scanf("%ls", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!=0){ Mensagem("Contato inexistente!!!"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problemas na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Um contato apagado nao pode ser alterado!!! \n\n"); return; } printf("\n\n Dados Atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Novos dados \n\n"); Ler_Pessoa(&x); fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Apagar_Pessoa(){ Pessoa x; long int nome; char resp; printf("Qual o nome do contato: "); scanf("%ls", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!= 0){ Mensagem("Contato inexistente!!!"); perror("fseek"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problema na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Contato ja esta apagado!!!\n\n"); } printf("\n\n Dados atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Apagar o contato (s/n)???: "); resp = getchar(); fflush(stdin); if(toupper(resp)!= 'S')return; x.Status= '*'; fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Listar(){ long int n_Linhas = 0; Pessoa reg; rewind(fp); printf("\n Nomes numero\n\n"); while(1){ if(fread(&reg, sizeof(reg), 1, fp)!= 1)break; if(reg.Status=='*') continue; Mostrar_Pessoa(reg); n_Linhas++; if(n_Linhas%20==0) Mensagem("\n\n Pressione <Enter> para continuar . . ."); } Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Pesquisar_Nome(char *s){ Pessoa reg; rewind(fp); while(fread(&reg, sizeof(Pessoa), 1, fp)) if(reg.Status!='*' && strstr(reg.nome, s)) Mostrar_Pessoa(reg); Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Ordenar_Nome(){ int fim, j; char aux[30]; Pessoa p[100]; int x=0; FILE *ft; ft= fopen(ARQ, "r+b"); while(fread(&p[x], sizeof(Pessoa), 1, ft) == 1 )x++; printf("---> %d nomes\n\n",x); for(fim=0; fim<x; fim++){ for(j=fim+1; j<x; j++){ if( strcmp(p[fim].nome, p[j].nome) > 0 ){ strcpy(aux, p[fim].nome); strcpy(aux, p[fim].nome); strcpy(p[fim].nome , p[j].nome); strcpy(p[ j].nome , aux); } } } printf("--- Lista telefonica ordenada ---\n\n"); for(j=0;j<x;j++){ if(p[j].nome!=" "){ printf("%d nome = %s\n",j+1,p[j].nome); } } fclose(ft); printf("\n\n Pressione <Enter> para continuar . . ."); getch(); } int main(int argc, char *argv[]) { char opcao; Inic(); while((opcao = Menu(MainMenu))!= OP_SAIR) switch(opcao){ case OP_INSERIR: Inserir_Pessoa(); break; case OP_ALTERAR: Alterar_Pessoa(); break; case OP_APAGAR: Apagar_Pessoa(); break; case OP_LISTAR: Listar(); break; case OP_PESQUISAR: { char string[BUFSIZ+1]; printf("Qual o nome a procurar: "); gets(string); fflush(stdin); Pesquisar_Nome(string); } break; case OP_ORDENAR: {Ordenar_Nome(); break; }; } fclose(fp); return 0; }
  2. é o (Dados.dat) que ta no inicio que estais falando? eu tentei apagar porém da muitos erros, mesmo com esse codigo modificado que você postou, não sei porque esta assim, sera que é porque to usando o Dev C++? adicionado 18 minutos depois muito estranho, o código ordena no code: blocks.
  3. De fato o codigo esta rodando, porém ele só ordena ate 2 nomes, quando tem mais ja não ordena.
  4. o código esta rodando ago #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <errno.h> #define ARQ "Dados.dat" #define OP_INSERIR '1' #define OP_ALTERAR '2' #define OP_APAGAR '3' #define OP_LISTAR '4' #define OP_PESQUISAR '5' #define OP_ORDENAR '6' #define OP_SAIR '0' #define OP_PESQ_NOME '1' int variavelglobal = 0; char *MainMenu[]={ "1. Inserir contato", "2. Alterar contato", "3. Apagar contato", "4. Listar contatos", "5. Pesquisar", "6. Ordenar", "0. Sair", NULL }; char *PesqMenu[]={ "1. Pesquisar por nome", NULL }; FILE *fp; typedef struct{ char nome[30+1]; char numero[17]; int indice; char Status; }Pessoa; void Mensagem(char *msg); int j, fim; void Ler_Pessoa(Pessoa *p){ for (j = 0; j < 31; j++){ for (j = 1; j < 31; j++) printf("Nome: "); scanf("%c", &p[j].nome); printf("Numero: "); scanf("%c", &p[j].numero); p->Status = ' '; fflush(stdin); }} int i = 0; void Mostrar_Pessoa(Pessoa p){ printf("%d: %-30s %17s\n", i, p.nome, p.numero); i += 1; } void Adiciona_Pessoa(Pessoa p){ fseek(fp, 0L, SEEK_END); if(fwrite(&p, sizeof(p), 1, fp)!=1) Mensagem("Adicionar pessoa: Falhou a escrita do contato"); } void Mensagem(char *msg){ printf(msg); getchar(); } void Inic(){ fp= fopen(ARQ, "r+b"); if(fp==NULL){ fp = fopen(ARQ, "w+b"); if(fp==NULL){ printf(stderr," Erro fatal: impossível criar arquivo de dados\n"); exit(1); } } } char Menu(char *opções[]){ int i; char ch; while(1){ printf("\n\n\n\n\n"); for(i=0; opções[i]!=NULL; i++) printf("\t\t%s\n\n",opções[i]); printf("\n\n\t\tOpcao: "); ch = getchar(); fflush(stdin); for(i=0; opções[i]!= NULL; i++) if(opções[i][0]==ch) return ch; } } void Inserir_Pessoa(){ Pessoa x; Ler_Pessoa(&x); x.indice = variavelglobal; variavelglobal++; Adiciona_Pessoa(x); } void Alterar_Pessoa(){ Pessoa x; long int nome; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!=0){ Mensagem("Contato inexistente ou problemas no posicionamento!!!"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problemas na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Um contato apagado não pode ser alterado!!! \n\n"); return; } printf("\n\n Dados Atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Novos dados \n\n"); Ler_Pessoa(&x); fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Apagar_Pessoa(){ Pessoa x; long int nome; char resp; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!= 0){ Mensagem("Contato inexistente ou problemas no contato!!!"); perror("fseek"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problema na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Contato já está apagado!!!\n\n"); } printf("\n\n Dados atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Apagar o contato (s/n)???: "); resp = getchar(); fflush(stdin); if(toupper(resp)!= 'S')return; x.Status= '*'; fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Listar(){ long int n_Linhas = 0; Pessoa reg; rewind(fp); while(1){ if(fread(&reg, sizeof(reg), 1, fp)!= 1)break; if(reg.Status=='*') continue; Mostrar_Pessoa(reg); n_Linhas++; if(n_Linhas%20==0) Mensagem("Pressione <Enter> para continuar . . ."); } Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Pesquisar_Nome(char *s){ Pessoa reg; rewind(fp); while(fread(&reg, sizeof(Pessoa), 1, fp)) if(reg.Status!='*' && strstr(reg.nome, s)) Mostrar_Pessoa(reg); Mensagem("\n\n Pressione <Enter> para continuar . . ."); /*No fim da listagem*/ } int fim, j; char aux; void Ordenar_Nome(){ Pessoa p[100]; int x=0; FILE *ft; ft= fopen(ARQ, "r+b"); while(fread(&p[x], sizeof(Pessoa), 1, ft) == 1 )x++; printf("%d nome = %s\n\n",x,p[0].nome);getch(); for(fim=0; fim<x; fim++){ for(j=fim+1; j<x; j++){ if( strcmp(p[fim].nome, p[j].nome) > 0){ strcpy(aux, p[fim].nome); strcpy(p[fim].nome, p[j].nome); strcpy(p[j].nome, aux); } } } printf("nomes ordenados :\n\n"); for(j=0;j<x;j++){ if(p[j].nome!=" "){ printf("%d nome = %s\n",j+1,p[j].nome); } } fclose(ft); } int main(int argc, char *argv[]) { char opcao; Inic(); while((opcao = Menu(MainMenu))!= OP_SAIR) switch(opcao){ case OP_INSERIR: Inserir_Pessoa(); break; case OP_ALTERAR: Alterar_Pessoa(); break; case OP_APAGAR: Apagar_Pessoa(); break; case OP_LISTAR: Listar(); break; case OP_PESQUISAR: { char string[BUFSIZ+1]; printf("Qual o nome a procurar: "); gets(string); fflush(stdin); Pesquisar_Nome(string); } break; case OP_ORDENAR: Ordenar_Nome(); break; } return 0; } ra, só não faz o pretendido.
  5. não entendi onde posso colocar isso, ou se tenho que substituir algo. também não compreendi como usar a referencia pessoa p. adicionado 1 minuto depois fiz algumas mudanças no código com base em parte dos comentários. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <errno.h> #define ARQ "Dados.dat" #define OP_INSERIR '1' #define OP_ALTERAR '2' #define OP_APAGAR '3' #define OP_LISTAR '4' #define OP_PESQUISAR '5' #define OP_ORDENAR '6' #define OP_SAIR '0' #define OP_PESQ_NOME '1' int variavelglobal = 0; char *MainMenu[]={ "1. Inserir contato", "2. Alterar contato", "3. Apagar contato", "4. Listar contatos", "5. Pesquisar", "6. Ordenar", "0. Sair", NULL }; char *PesqMenu[]={ "1. Pesquisar por nome", NULL }; FILE *fp; typedef struct{ char nome[30+1]; char numero[17]; int indice; char Status; }Pessoa; void Mensagem(char *msg); int j, fim; void Ler_Pessoa(Pessoa *p){ for (j = 0; j < 31; j++){ for (j = 1; j < 31; j++) printf("Nome: "); scanf("%c", &p[j].nome); printf("Numero: "); scanf("%c", &p[j].numero); p->Status = ' '; fflush(stdin); }} int i = 0; void Mostrar_Pessoa(Pessoa p){ printf("%d: %-30s %17s\n", i, p.nome, p.numero); i += 1; } void Adiciona_Pessoa(Pessoa p){ fseek(fp, 0L, SEEK_END); if(fwrite(&p, sizeof(p), 1, fp)!=1) Mensagem("Adicionar pessoa: Falhou a escrita do contato"); } void Mensagem(char *msg){ printf(msg); getchar(); } void Inic(){ fp= fopen(ARQ, "r+b"); if(fp==NULL){ fp = fopen(ARQ, "w+b"); if(fp==NULL){ printf(stderr," Erro fatal: impossível criar arquivo de dados\n"); exit(1); } } } char Menu(char *opções[]){ int i; char ch; while(1){ printf("\n\n\n\n\n"); for(i=0; opções[i]!=NULL; i++) printf("\t\t%s\n\n",opções[i]); printf("\n\n\t\tOpcao: "); ch = getchar(); fflush(stdin); for(i=0; opções[i]!= NULL; i++) if(opções[i][0]==ch) return ch; } } void Inserir_Pessoa(){ Pessoa x; Ler_Pessoa(&x); x.indice = variavelglobal; variavelglobal++; Adiciona_Pessoa(x); } void Alterar_Pessoa(){ Pessoa x; long int nome; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!=0){ Mensagem("Contato inexistente ou problemas no posicionamento!!!"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problemas na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Um contato apagado não pode ser alterado!!! \n\n"); return; } printf("\n\n Dados Atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Novos dados \n\n"); Ler_Pessoa(&x); fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Apagar_Pessoa(){ Pessoa x; long int nome; char resp; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!= 0){ Mensagem("Contato inexistente ou problemas no contato!!!"); perror("fseek"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problema na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Contato já está apagado!!!\n\n"); } printf("\n\n Dados atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Apagar o contato (s/n)???: "); resp = getchar(); fflush(stdin); if(toupper(resp)!= 'S')return; x.Status= '*'; fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Listar(){ long int n_Linhas = 0; Pessoa reg; rewind(fp); while(1){ if(fread(&reg, sizeof(reg), 1, fp)!= 1)break; if(reg.Status=='*') continue; Mostrar_Pessoa(reg); n_Linhas++; if(n_Linhas%20==0) Mensagem("Pressione <Enter> para continuar . . ."); } Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Pesquisar_Nome(char *s){ Pessoa reg; rewind(fp); while(fread(&reg, sizeof(Pessoa), 1, fp)) if(reg.Status!='*' && strstr(reg.nome, s)) Mostrar_Pessoa(reg); Mensagem("\n\n Pressione <Enter> para continuar . . ."); /*No fim da listagem*/ } int fim, j; void Ordenar_Nome() { for (fim = 31; fim >= 0; fim--) for (j = 0; j <= fim; j++) { if (nome[j] > nome[j + 1]) { aux = nome[j]; nome[j] = nome[j + 1]; nome[j + 1] = aux; } } } int main(int argc, char *argv[]) { char opcao; Inic(); while((opcao = Menu(MainMenu))!= OP_SAIR) switch(opcao){ case OP_INSERIR: Inserir_Pessoa(); break; case OP_ALTERAR: Alterar_Pessoa(); break; case OP_APAGAR: Apagar_Pessoa(); break; case OP_LISTAR: Listar(); break; case OP_PESQUISAR: { char string[BUFSIZ+1]; printf("Qual o nome a procurar: "); gets(string); fflush(stdin); Pesquisar_Nome(string); } break; case OP_ORDENAR: Ordenar_Nome(); break; } return 0; }
  6. Fiz algumas mudanças no codigo. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <errno.h> #define ARQ "Dados.dat" #define OP_INSERIR '1' #define OP_ALTERAR '2' #define OP_APAGAR '3' #define OP_LISTAR '4' #define OP_PESQUISAR '5' #define OP_ORDENAR '6' #define OP_SAIR '0' #define OP_PESQ_NOME '1' int variavelglobal = 0; char *MainMenu[]={ "1. Inserir contato", "2. Alterar contato", "3. Apagar contato", "4. Listar contatos", "5. Pesquisar", "6. Ordenar", "0. Sair", NULL }; char *PesqMenu[]={ "1. Pesquisar por nome", NULL }; FILE *fp; typedef struct{ char nome[30+1]; char numero[17]; int indice; char Status; }Pessoa; void Mensagem(char *msg); int i, fim; void Ler_Pessoa(Pessoa *p){ for (i = 0; i < 31; i++){ for (i = 1; i < 31; i++) printf("Nome: "); gets(p->nome[i]); printf("Numero: "); gets(p->numero); p->Status = ' '; fflush(stdin); }} int i = 0; void Mostrar_Pessoa(Pessoa p){ printf("%d: %-30s %17s\n", i, p.nome, p.numero); i += 1; } void Adiciona_Pessoa(Pessoa p){ fseek(fp, 0L, SEEK_END); if(fwrite(&p, sizeof(p), 1, fp)!=1) Mensagem("Adicionar pessoa: Falhou a escrita do contato"); } void Mensagem(char *msg){ printf(msg); getchar(); } void Inic(){ fp= fopen(ARQ, "r+b"); if(fp==NULL){ fp = fopen(ARQ, "w+b"); if(fp==NULL){ printf(stderr," Erro fatal: impossível criar arquivo de dados\n"); exit(1); } } } char Menu(char *opções[]){ int i; char ch; while(1){ printf("\n\n\n\n\n"); for(i=0; opções[i]!=NULL; i++) printf("\t\t%s\n\n",opções[i]); printf("\n\n\t\tOpcao: "); ch = getchar(); fflush(stdin); for(i=0; opções[i]!= NULL; i++) if(opções[i][0]==ch) return ch; } } void Inserir_Pessoa(){ Pessoa x; Ler_Pessoa(&x); x.indice = variavelglobal; variavelglobal++; Adiciona_Pessoa(x); } void Alterar_Pessoa(){ Pessoa x; long int nome; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!=0){ Mensagem("Contato inexistente ou problemas no posicionamento!!!"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problemas na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Um contato apagado não pode ser alterado!!! \n\n"); return; } printf("\n\n Dados Atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Novos dados \n\n"); Ler_Pessoa(&x); fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Apagar_Pessoa(){ Pessoa x; long int nome; char resp; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!= 0){ Mensagem("Contato inexistente ou problemas no contato!!!"); perror("fseek"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problema na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Contato já está apagado!!!\n\n"); } printf("\n\n Dados atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Apagar o contato (s/n)???: "); resp = getchar(); fflush(stdin); if(toupper(resp)!= 'S')return; x.Status= '*'; fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Listar(){ long int n_Linhas = 0; Pessoa reg; rewind(fp); while(1){ if(fread(&reg, sizeof(reg), 1, fp)!= 1)break; if(reg.Status=='*') continue; Mostrar_Pessoa(reg); n_Linhas++; if(n_Linhas%20==0) Mensagem("Pressione <Enter> para continuar . . ."); } Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Pesquisar_Nome(char *s){ Pessoa reg; rewind(fp); while(fread(&reg, sizeof(Pessoa), 1, fp)) if(reg.Status!='*' && strstr(reg.nome, s)) Mostrar_Pessoa(reg); Mensagem("\n\n Pressione <Enter> para continuar . . ."); /*No fim da listagem*/ } void Ordenar_Nome() { for (fim = 8; fim >= 0; fim--) for (i = 0; i <= fim; i++) { if (nome[i] > nome[i + 1]) { aux = nome[i]; nome[i] = nome[i + 1]; nome[i + 1] = aux; } } } int main(int argc, char *argv[]) { char opcao; Inic(); while((opcao = Menu(MainMenu))!= OP_SAIR) switch(opcao){ case OP_INSERIR: Inserir_Pessoa(); break; case OP_ALTERAR: Alterar_Pessoa(); break; case OP_APAGAR: Apagar_Pessoa(); break; case OP_LISTAR: Listar(); break; case OP_PESQUISAR: { char string[BUFSIZ+1]; printf("Qual o nome a procurar: "); gets(string); fflush(stdin); Pesquisar_Nome(string); } break; case OP_ORDENAR: Ordenar(); break; } return 0; } porém a ordenação que inclui dessa vez continua não funcionando.
  7. Não consigo colocar um código de ordenação nesse código, nunca funciona, alguém sabe como fazer? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <ctype.h> #include <errno.h> #define ARQ "Dados.dat" #define OP_INSERIR '1' #define OP_ALTERAR '2' #define OP_APAGAR '3' #define OP_LISTAR '4' #define OP_PESQUISAR '5' #define OP_ORDENAR '6' #define OP_SAIR '0' #define OP_PESQ_NOME '1' int variavelglobal = 0; char *MainMenu[]={ "1. Inserir contato", "2. Alterar contato", "3. Apagar contato", "4. Listar contatos", "5. Pesquisar", "6. Ordenar", "0. Sair", NULL }; char *PesqMenu[]={ "1. Pesquisar por nome", NULL }; FILE *fp; typedef struct{ char nome[30+1]; char numero[17]; int indice; char Status; }Pessoa; void Mensagem(char *msg); void Ler_Pessoa(Pessoa *p){ printf("Nome: "); gets(p->nome); printf("Numero: "); gets(p->numero); p->Status = ' '; fflush(stdin); } void Mostrar_Pessoa(Pessoa p){ printf("%d: %-30s %17s\n", p.indice, p.nome, p.numero); } void Adiciona_Pessoa(Pessoa p){ fseek(fp, 0L, SEEK_END); if(fwrite(&p, sizeof(p), 1, fp)!=1) Mensagem("Adicionar pessoa: Falhou a escrita do contato"); } void Mensagem(char *msg){ printf(msg); getchar(); } void Inic(){ fp= fopen(ARQ, "r+b"); if(fp==NULL){ fp = fopen(ARQ, "w+b"); if(fp==NULL){ printf(stderr," Erro fatal: impossível criar arquivo de dados\n"); exit(1); } } } char Menu(char *opções[]){ int i; char ch; while(1){ printf("\n\n\n\n\n"); for(i=0; opções[i]!=NULL; i++) printf("\t\t%s\n\n",opções[i]); printf("\n\n\t\tOpcao: "); ch = getchar(); fflush(stdin); for(i=0; opções[i]!= NULL; i++) if(opções[i][0]==ch) return ch; } } void Inserir_Pessoa(){ Pessoa x; Ler_Pessoa(&x); x.indice = variavelglobal; variavelglobal++; Adiciona_Pessoa(x); } void Alterar_Pessoa(){ Pessoa x; long int nome; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!=0){ Mensagem("Contato inexistente ou problemas no posicionamento!!!"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problemas na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Um contato apagado não pode ser alterado!!! \n\n"); return; } printf("\n\n Dados Atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Novos dados \n\n"); Ler_Pessoa(&x); fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Apagar_Pessoa(){ Pessoa x; long int nome; char resp; printf("Qual o nome do contato: "); scanf("%ld", &nome); fflush(stdin); if(fseek(fp, (nome)*sizeof(Pessoa), SEEK_SET)!= 0){ Mensagem("Registro inexistente ou problemas no contato!!!"); perror("fseek"); return; } if(fread(&x, sizeof(Pessoa), 1, fp)!= 1){ Mensagem("Problema na leitura do contato!!!"); return; } if(x.Status=='*'){ Mensagem("Contato já está apagado!!!\n\n"); } printf("\n\n Dados atuais \n\n"); Mostrar_Pessoa(x); printf("\n\n Apagar o contato (s/n)???: "); resp = getchar(); fflush(stdin); if(toupper(resp)!= 'S')return; x.Status= '*'; fseek(fp, -(long) sizeof(Pessoa), SEEK_CUR); fwrite(&x, sizeof(Pessoa), 1, fp); fflush(fp); } void Listar(){ long int n_Linhas = 0; Pessoa reg; rewind(fp); while(1){ if(fread(&reg, sizeof(reg), 1, fp)!= 1)break; if(reg.Status=='*') continue; Mostrar_Pessoa(reg); n_Linhas++; if(n_Linhas%20==0) Mensagem("Pressione <Enter> para continuar . . ."); } Mensagem("\n\n Pressione <Enter> para continuar . . ."); } void Pesquisar_Nome(char *s){ Pessoa reg; rewind(fp); while(fread(&reg, sizeof(Pessoa), 1, fp)) if(reg.Status!='*' && strstr(reg.nome, s)) Mostrar_Pessoa(reg); Mensagem("\n\n Pressione <Enter> para continuar . . ."); /*No fim da listagem*/ } void Ordenar_Nome() { } int main(int argc, char *argv[]) { char opcao; Inic(); while((opcao = Menu(MainMenu))!= OP_SAIR) switch(opcao){ case OP_INSERIR: Inserir_Pessoa(); break; case OP_ALTERAR: Alterar_Pessoa(); break; case OP_APAGAR: Apagar_Pessoa(); break; case OP_LISTAR: Listar(); break; case OP_PESQUISAR: { char string[BUFSIZ+1]; printf("Qual o nome a procurar: "); gets(string); fflush(stdin); Pesquisar_Nome(string); } break; case OP_ORDENAR: { break; }; } return 0; }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!