Remove() and Rename() functions do not work!

Asked

Viewed 144 times

1

Hello, I’m doing a program on has a part of it that should delete a client and the data of such that are in a file . txt! So far so good, I create a new file with a different name that copies all the clients that were in the other file, except the client to be deleted! bad after having done this I need to delete the first file and rename the second to the name of the first!

Then I did a search and I found these two functions that promise to delete and rename files, happens when trying to use them they neither erase nor rename the files I tried very careful, but nothing worked, even when I go to see the output of functions, instead of returning zero return -1. If anyone can help I would be very grateful!

Thanks in advance!

void copia_arquivo(FILE *file1, FILE *file2, char cpf[15])

{

char confirma_nome2[15];
char confirma_ultimoNome2[15];
char confirma_dataNasc2[11];
char confirma_cpf2[15];
int confirma_numero2;

 while (fscanf(file1, "%s %s  %s %i %s", confirma_nome2, confirma_ultimoNome2, confirma_dataNasc2, &confirma_numero2, confirma_cpf2) != EOF)
{
    if (strstr(cpf, confirma_cpf2) == 0)
    {
        fprintf(file2, "%s %s  %s %i %s\n", confirma_nome2, confirma_ultimoNome2, confirma_dataNasc2, confirma_numero2, confirma_cpf2);
    }
}
fclose(file1);
fclose(file2);
remove("clientes\\client.txt");
rename("clientes\\client2.txt", "clientes\\client.txt");
}
  • Creating a new file and removing the old one is not a good solution. It is even better to open the file normally for writing(other than append) and write directly the information you want.

  • it turns out that what I want is not to write and delete, erase the customer data! I think there is no other way to do than this.

  • If Voce is in Windows environment you have to pass the complete path of the files in the functions remove and rename.

  • You always have a thousand different ways to do it, and the one you chose isn’t the best one at all. It would be better to read the clients from the file to memory in a list/array of clients, and to remove or read only the ones that matter (not reading/storing the ones you want to remove). Then re-writes them to the same file, that the removed ones will no longer be written again.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.