luiscastro Posted May 20, 2009 at 11:58 AM Report #265763 Posted May 20, 2009 at 11:58 AM Pessoal....tenho este codigo: #include<stdio.h> #include<stdlib.h> #include<string.h> #include <conio.h> #include <ctype.h> #define LIMITE 100 void cabecalho() { printf("\n\n\n\t\t\t***** BUZZ GAME *****\n\n\n"); } char grupo1[100]; typedef struct { int ID; char pergunta[50]; char resposta1[20]; char resposta2[20]; char resposta3[20]; char resposta4[20]; int RespostaCorreta; }TPERGUNTA; /*Dados de uma pergunta */ typedef TPERGUNTA VECT[100]; /*Dados de 1000 alunos */ /*Declaracao previa das funções*/ TPERGUNTA AcrescentarPergunta(void); void ListarPergunta(TPERGUNTA pergunta); void ListarPerguntas(VECT perguntas, int nperguntas); void Guardar_Fich_Bin(VECT perguntas, int nperguntas); void Ler_Fich_Bin(VECT perguntas, int *nperguntas); void AlterarPergunta(VECT perguntas, int nperguntas); void RemoverPergunta(); void AdmJogo(); void inicio(); TPERGUNTA AcrescentarPergunta(void) { char sexo; TPERGUNTA pergunta; printf("Introduza o ID da pergunta: "); fflush(stdin); scanf("%d", & pergunta.ID); printf("Introduza a pergunta: "); fflush(stdin); gets(pergunta.pergunta); printf("Introduza a 1 resposta "); fflush(stdin); gets(pergunta.resposta1); printf("Introduza a 2 resposta "); fflush(stdin); gets(pergunta.resposta2); printf("Introduza a 3 resposta "); fflush(stdin); gets(pergunta.resposta3); printf("Introduza a 4 resposta "); fflush(stdin); gets(pergunta.resposta4); printf("Introduza o numero da resposta correta: "); fflush(stdin); scanf("%d", & pergunta.RespostaCorreta); return pergunta; FILE *fp; fp=fopen("alunos.txt","w"); if (fp!=NULL) { fprintf(fp,"\n"); fclose(fp); } fclose(fp); system("pause"); } void novo_Int(VECT perguntas, int novo_int) { int i; perguntas[i].ID=novo_int; } void AlterarPergunta(VECT perguntas, int nperguntas) { int i,conf,opcao; printf("Introduza o ID da pergunta a alterar: "); fflush(stdin); scanf("%d",&conf); for(i=0;i<nperguntas;i++) { if(conf==perguntas[i].ID) { printf("%d",perguntas[i].ID); fflush(stdin); printf("%s",perguntas[i].pergunta); fflush(stdin); printf("%s",perguntas[i].resposta1); fflush(stdin); printf("%s",perguntas[i].resposta2); fflush(stdin); printf("%s",perguntas[i].resposta3); fflush(stdin); printf("%s",perguntas[i].resposta4); fflush(stdin); printf("%d",perguntas[i].RespostaCorreta); fflush(stdin); int novo_int; char novo_array; printf("\n\nQue parte pretende alterar?\n\n\t1 -> ID\n\t2 -> Pergunta\n\t3 -> Resposta 1\n\t4 -> Resposta 2\n\t5 -> Resposta 3\n\t6 -> Resposta 4\n\t7 -> Resposta Correta\n"); scanf("%d",&opcao); if(opcao==1) { printf("Introduza o novo ID: "); scanf("%d",&novo_int); perguntas[i].ID=novo_int; break; } } else { printf("pppppp"); break; } printf("aki"); //se nao entrar nas condixoes entra aki e ja se ve k nao entrou } system("pause"); } void RemoverPergunta(VECT perguntas, int nperguntas) { int i,conf; printf("Introduza o ID da pergunta a apagar: "); fflush(stdin); scanf("%d",&conf); for(i=0;i<nperguntas;i++) { if(conf==perguntas[i].ID) { } } } void ListarPergunta(TPERGUNTA pergunta) { printf("\n ID: %d",pergunta.ID); fflush(stdin); printf("\n Pergunta: %s",pergunta.pergunta); fflush(stdin); printf("\n Resposta 1: %s",pergunta.resposta1); fflush(stdin); printf("\n Resposta 2: %s",pergunta.resposta2); fflush(stdin); printf("\n Resposta 3: %s",pergunta.resposta3); fflush(stdin); printf("\n Resposta 4: %s",pergunta.resposta4); fflush(stdin); printf("\n Resposta Correta: %d",pergunta.RespostaCorreta); fflush(stdin); } void ListarPerguntas(VECT perguntas, int nperguntas) { int i; for(i=0;i<nperguntas;i++) { printf("\n--------------------------------------------"); ListarPergunta(perguntas[i]); } printf("\n--------------------------------------------\n\n"); system("pause"); } void Guardar_Fich_Bin(VECT perguntas, int nperguntas) { FILE *fp; if ((fp=fopen("alunos.txt","wb"))==NULL) { fprintf(stderr,"\n Erro na abertura do ficheiro para escrita"); getch(); } else { fwrite(&nperguntas,sizeof(char),1,fp); fwrite(perguntas,sizeof(TPERGUNTA),nperguntas,fp); fclose(fp); } system("pause"); } void Ler_Fich_Bin(VECT perguntas, int *nperguntas) { FILE *fp; (*nperguntas)=0; if ((fp=fopen("alunos.txt","rb"))==NULL) { fprintf(stderr,"\n Erro na abertura do ficheiro para leitura"); getch(); } else { fread(nperguntas,sizeof(char),1,fp); printf("\n N. perguntas: %d ", *nperguntas); fread(perguntas,sizeof(TPERGUNTA),*nperguntas,fp); fclose(fp); } system("pause"); } void AdmJogo() { VECT v_perguntas; /*Vector que vai armazenar os dados de todos os alunos*/ int opcao, nperguntas=0; do { printf("\n\n\tBem vindo Sr. Admistrador\n\n"); printf("\n\nQue deseja fazer?\n\n"); printf("1 - Carregar ficheiro armazem de perguntas?\n\n"); printf("2 - Adicionar uma pergunta?\n\n"); printf("3 - Alterar Pergunta?\n\n"); printf("4 - Ver todas as perguntas?\n\n"); printf("5 - Apagar uma pergunta?\n\n"); printf("6 - Guardar perguntas?\n\n"); scanf("%d", &opcao); switch(opcao) { case 1: Ler_Fich_Bin(v_perguntas,&nperguntas); break; case 2: v_perguntas[nperguntas++]=AcrescentarPergunta(); break; case 3: AlterarPergunta(v_perguntas, nperguntas); break; case 4: ListarPerguntas(v_perguntas, nperguntas); break; case 5: void RemoverPergunta(); break; case 6: Guardar_Fich_Bin(v_perguntas, nperguntas); break; default: printf("\n\nOPCAO INVALIDA..... ESCOLHA OUTRA\n\n"); } } while(opcao!=5); } void inicio() { int opcao; do { int password=1,passe; printf("\n\n\t1 - ADMISTRADOR\n\n\t2 - JOGAR\n\n"); scanf("%d",&opcao); switch(opcao) { case 1: printf("\nIntroduza a password "); fflush(stdin); scanf("%d",&passe); if (passe==password) { AdmJogo(); } else printf("\nPassword incorreta\n"); inicio(); break; default: printf("\n\a\aOPCAO INVALIDA..... ESCOLHA OUTRA\n"); } } while(opcao!=1); } main() { cabecalho(); inicio(); system("pause"); } como se ve a função remover RemoverPergunta não esta terminada. como posso apagar uma linha do ficheiro no meu programa? Sabendo que o ficheiro esta carregado para o programa...
TheDark Posted May 20, 2009 at 12:04 PM Report #265765 Posted May 20, 2009 at 12:04 PM Se já tens o conteúdo do ficheiro na memória, basta reescreveres todas as perguntas para o ficheiro menos a que queres apagar. Desaparecido.
luiscastro Posted May 20, 2009 at 12:12 PM Author Report #265766 Posted May 20, 2009 at 12:12 PM como posso fazer isso?
edsousa Posted May 20, 2009 at 01:17 PM Report #265790 Posted May 20, 2009 at 01:17 PM Tens um erro na função de leitura e escrita ao ler/guardar o no de perguntas. Onde se lê fread(nperguntas,sizeof(char),1,fp); deveria ser fread(nperguntas,sizeof(int),1,fp); A escrita ficaria algo como fwrite(&nperguntas,sizeof(int),1,fp); Tharis Fan ClubMay Tharis bless you
luiscastro Posted May 20, 2009 at 01:26 PM Author Report #265792 Posted May 20, 2009 at 01:26 PM ja consertei....
luiscastro Posted May 20, 2009 at 03:27 PM Author Report #265831 Posted May 20, 2009 at 03:27 PM como reescrevo todas as perguntas para o ficheiro menos a que quero apagar???
edsousa Posted May 20, 2009 at 03:47 PM Report #265842 Posted May 20, 2009 at 03:47 PM com a alteração que eu indiquei para a escrita e o resto da função que já tens não funciona? Tharis Fan ClubMay Tharis bless you
TheDark Posted May 20, 2009 at 10:47 PM Report #265992 Posted May 20, 2009 at 10:47 PM void RemoverPergunta(VECT perguntas, int nperguntas) { int i,conf; printf("Introduza o ID da pergunta a apagar: "); fflush(stdin); scanf("%d",&conf); for(i=0;i<nperguntas;i++) { if(conf==perguntas[i].ID) { } } } Curiosamente, já tens tudo o que precisas, só precisas de alterar a condição e meter lá o fwrite. Ali no if, pensa assim: se conf for diferente de perguntas[ i ].ID, escreve para o ficheiro, caso contrário não escreve nada. Desaparecido.
luiscastro Posted May 21, 2009 at 02:49 PM Author Report #266161 Posted May 21, 2009 at 02:49 PM oh men....ja fiz de todas as maneiras tou com o codigo assim : void RemoverPergunta(VECT perguntas, int nperguntas, char nomegrupo[]) { int i,conf; printf("Introduza o ID da pergunta a apagar: "); fflush(stdin); scanf("%d",&conf); for(i=0;i<nperguntas;i++) { if(conf!=perguntas[i].ID) { } } } poe-me so aki o fwrite agora....so me falta esta função para entregar amanha........
luiscastro Posted May 21, 2009 at 08:45 PM Author Report #266267 Posted May 21, 2009 at 08:45 PM ta resolvido...mas fix para retroceder uma posição na memoria a partir da posição onde estava o ID da perg a eliminar
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now