Jump to content

Recommended Posts

Posted

Boas pessoal, estou a tentar eliminar um registo de um ficheiro, mas não está eliminar, podem ajudar?


void Eliminar()
{

char estado = 'A';
int numero;
int n;
printf("Numero do aluno?");
scanf("%d", &numero);

fich = fopen("alunos.DAT","r+b");

if (fich== NULL) {
printf("\nErro ao abrir o ficheiro\n");
return;
}

fseek(fich, 0, SEEK_END);
n = ftell(fich) / sizeof(a);
fseek(fich, 0, SEEK_SET);

int pos = 0;
int i;
for (i = 0; i < n; i++) {
fread(&a, sizeof(a), 1, fich);

if (estado == 'A')
if (a.codigo == numero) {
pos = i;
printf("%d\n",a.codigo);
printf("%s\n",a.nome);
printf("%s\n",a.curso);
break;
}
}
if (i < n) {

int eliminar;
printf("Pretende eliminar o aluno?\n");
printf("   1 - Sim           0 - Nao\n");
scanf("%d", & eliminar);
if (eliminar == 0){
return;
}else
{

fseek(fich, pos * sizeof(a), SEEK_END);
fread(&a, sizeof(a), 1, fich);
fseek(fich, pos * sizeof(a), SEEK_SET);
fwrite(&a, sizeof(a), 1, fich);
printf("O Aluno foi eliminado com sucesso\n");
}
fclose(fich);
}

}
Posted

explica o que estás a fazer em cada uma das seguintes linhas :

fseek(fich, pos * sizeof(a), SEEK_END);
fread(&a, sizeof(a), 1, fich);
fseek(fich, pos * sizeof(a), SEEK_SET);
fwrite(&a, sizeof(a), 1, fich);

Pretendo calcular o numero de registros fazendo a respectiva leitura

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.