How to delete a file (not txt) in C?

0

I'm creating a program that puts each student's grades in a different file. The user puts the code of the student and this code turns the name of a file that is not txt (I do not know how does it do txt). But I'm not able to delete this file. For example, the user will enter the student code and I want the file with that code to be excluded. I've tried using the remove () function, but it only works for the txt file. I also tried using the unlink () function but I did not quite understand how it works.

Ha ha, obg !!

void apagar()
{

char nusp[8];

printf("--------- CONSULTA ---------\n\n");

printf("Digite o NUSP do aluno(a): ");

gets(nusp);
FILE *consultar;
consultar = fopen(nusp,"r");
if (consultar==NULL)
{
    printf("\nAluno ainda nao cadastrado.");

}
else
{
    printf("\n");
    remove(nusp);
    printf("\nAluno removido com sucesso!\n\n");
    system("pause");
}
}
    
asked by anonymous 22.07.2018 / 23:28

1 answer

1

You can not delete a file that is open. Close the file with fclose(consultar); before attempting to delete it.

And please do not use gets never. Reasons not to use gets I explain in this answer and I also talk about it nest another . Use fgets(nusp, 8, stdin); instead of gets(nusp); .

    
23.07.2018 / 01:08