How can I delete content from an array in a .txt C?

2

I have a function to add clients through an array, these clients are stored in a .txt. (as I can show the clients in the txt with line breaks) Once I add the clients I want to be able to eliminate them and for that I create the function to eliminate clients, which should look for the user's ID and eliminate the whole user. Ex: 4042923H, name antonio. You look for the ID 4042923H and delete both the ID and the name.

The complete code is at the following link: link

The function with which I add clients is the following:

void aniadirCliente()
{

    if(clientes[n].dni[0]=='
void eliminarCliente(){

 if(clientes[n].dni[0]=='
void aniadirCliente()
{

    if(clientes[n].dni[0]=='
void eliminarCliente(){

 if(clientes[n].dni[0]=='%pre%')
        {
            printf("\nIntroduce el nombre del archivo: \nNombre por defecto: clientes.txt\n\n");
            fflush(stdin);
            gets(nom_archivo);
            //Cuando abrimos el fichero le tenemos que poner rw para que podamos escribir y leer.
            archivo=fopen(nom_archivo,"rw");
            if ((archivo = fopen(nom_archivo, "rw")) == NULL) {
                fprintf(stderr, "\n\nEl archivo no existe.");
            }else{
                int i, y;
                char delcliente;
                for(i=0; i<max_clientes;i++){
            fscanf(archivo,"\n %s %s %s %s",clientes[i].dni,clientes[i].nombre,clientes[i].apellidos,clientes[i].direccion);
        }
                printf("Introduce el dni del cliente que deseas eliminar");
                scanf("%s", &delcliente);
                if(delcliente==clientes[i].dni){

                    printf("Cliente Eliminado");
                    //How to delete all of the client
                }
                else{
                    printf("El dni introducido no coincide");
                }

                }
        }


}
'){ printf("\nIntroduce el nombre del archivo: \nNombre por defecto: clientes.txt\n\n"); gets(nom_archivo); fflush(stdin); //Cuando abrimos el fichero le tenemos que poner rw para que podamos escribir y leer. archivo=fopen(nom_archivo,"rw"); if ((archivo = fopen(nom_archivo, "rw")) == NULL) { fprintf(stderr, "\n\nEl archivo no existe."); system("cls"); //En windows limpia pantalla }else { int i; printf("\n\nArchivo cargado correctamente.|\n"); for(i=0; i<max_clientes;i++){ fscanf(archivo,"\n %s %s %s %s",clientes[i].dni,clientes[i].nombre,clientes[i].apellidos,clientes[i].direccion); } for(i=0;i<max_clientes;i++) { if(clientes[i].dni[0]=='%pre%') { //Crear un nuevo cliente, lo mismo que antes los espacios con _ puts("DNI:"); scanf("%s", &clientes[i].dni); fprintf(archivo, "%s", clientes[i].dni); puts("Nombre:"); scanf("%s", &clientes[i].nombre); fprintf(archivo, "%s", &clientes[i].nombre); puts("Apellidos:"); scanf("%s", &clientes[i].apellidos); fprintf(archivo, "%s", &clientes[i].apellidos); puts("Direccion:"); scanf("%s", &clientes[i].direccion); fprintf(archivo, "%s", &clientes[i].direccion); break; } } } } fclose(archivo); system("cls"); }
') { printf("\nIntroduce el nombre del archivo: \nNombre por defecto: clientes.txt\n\n"); fflush(stdin); gets(nom_archivo); //Cuando abrimos el fichero le tenemos que poner rw para que podamos escribir y leer. archivo=fopen(nom_archivo,"rw"); if ((archivo = fopen(nom_archivo, "rw")) == NULL) { fprintf(stderr, "\n\nEl archivo no existe."); }else{ int i, y; char delcliente; for(i=0; i<max_clientes;i++){ fscanf(archivo,"\n %s %s %s %s",clientes[i].dni,clientes[i].nombre,clientes[i].apellidos,clientes[i].direccion); } printf("Introduce el dni del cliente que deseas eliminar"); scanf("%s", &delcliente); if(delcliente==clientes[i].dni){ printf("Cliente Eliminado"); //How to delete all of the client } else{ printf("El dni introducido no coincide"); } } } }
'){ printf("\nIntroduce el nombre del archivo: \nNombre por defecto: clientes.txt\n\n"); gets(nom_archivo); fflush(stdin); //Cuando abrimos el fichero le tenemos que poner rw para que podamos escribir y leer. archivo=fopen(nom_archivo,"rw"); if ((archivo = fopen(nom_archivo, "rw")) == NULL) { fprintf(stderr, "\n\nEl archivo no existe."); system("cls"); //En windows limpia pantalla }else { int i; printf("\n\nArchivo cargado correctamente.|\n"); for(i=0; i<max_clientes;i++){ fscanf(archivo,"\n %s %s %s %s",clientes[i].dni,clientes[i].nombre,clientes[i].apellidos,clientes[i].direccion); } for(i=0;i<max_clientes;i++) { if(clientes[i].dni[0]=='%pre%') { //Crear un nuevo cliente, lo mismo que antes los espacios con _ puts("DNI:"); scanf("%s", &clientes[i].dni); fprintf(archivo, "%s", clientes[i].dni); puts("Nombre:"); scanf("%s", &clientes[i].nombre); fprintf(archivo, "%s", &clientes[i].nombre); puts("Apellidos:"); scanf("%s", &clientes[i].apellidos); fprintf(archivo, "%s", &clientes[i].apellidos); puts("Direccion:"); scanf("%s", &clientes[i].direccion); fprintf(archivo, "%s", &clientes[i].direccion); break; } } } } fclose(archivo); system("cls"); }

And the function to eliminate clients : clients [i] .dni, clients [i] .name, clients [i] .lastname, clients [i] .direction.

%pre%     
asked by Trackless 18.03.2018 в 22:47
source

1 answer

1

The simplest way is to rewrite all the information in the file, omitting the one corresponding to the DNI to be deleted:

rewind(archivo); // Regresa la posición del archivo al inicio
if (delcliente == clientes[i].dni) {
    for (int pos = 0; pos < max_clientes; ++pos) {
        if (pos != i) 
            fprintf(archivo,"\n %s %s %s %s",clientes[i].dni,clientes[i].nombre,clientes[i].apellidos,clientes[i].direccion);     else
            fprintf(archivo,"\n %s %s %s %s", "", "", "", "");
    }
}

However, I think there is a problem in which you write and read strings in the file. Your program has the typical structure that supports random access read / write except for one thing: Reading and writing is not done in binary form but in the form of characters which is very likely not going to work as expected.

    
answered by 20.03.2018 / 07:23
source