elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++  (Leído 2,503 veces)
Abril7

Desconectado Desconectado

Mensajes: 22


Ver Perfil
Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++
« en: 11 Septiembre 2017, 16:32 pm »

Hola, estoy haciendo un codigo que captura, muestra, elimina, modifica y busca datos en un archivo, este a su vez incluye un campo que es otro archivo, son 2 clases, una esta hecha con delimitadores y la otra con campos de dimension. El capturar, mostrar y buscar estan funcionando, y el eliminar casi, pero tengo una duda, en la funcion eliminar no se esta creando mi archivo auxiliar para respaldar los datos nuevos sin el registro que queria eliminar y no entiendo por que si para la otra clase si funciona y se crea el archivo, agradeceria mucho si alguien me puede decir que hago mal. MUCHAS GRACIAS.

Estas son mis clases y librerias:

Código:
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstring>
using namespace std;

class Profile
{

public:
    string captureP();
    string showP();
    void deleteUP();
    void searchUP();
    void editP();

    char idProfile[30];
    char level[30];
    char type[30];
};

class User: public Profile
{

public:
    void capture();
    void show();
    void deleteU();
    void searchU();
    void edit();

    char idUser[30];
    char name[30];
    char mail[40];
    char tel[20];
    char ranking[30];
    char idProfile[30];
};

Esa es mi funcion eliminar:

Código:
void User::deleteU()
{
    int op = 0;
    int ii,ll,tt;
    cout<<"What do you want to do? 1)Search by IDUSER 2) Search by IDPROFILE"<<endl;
    cin>>op;

    if(op == 1)
    {
        string del;
        cout<<"Which idUser do you want to find?"<<endl;
        cin.ignore();
        getline(cin,del);
        bool found = false;

        ifstream read;
        ifstream read2;
        ofstream aux1;
        ofstream aux2;

        int i = 0;
        int es = 0;
        aux1.open("Aux.txt",ios::out);
        aux2.open("Aux2.txt",ios::out);
        read.open("User.txt", ios::in);
        read2.open("Profile.txt", ios::in);
        char line[200];
        read.getline(line,sizeof(line));
        while(!read.eof())
        {
            char *pointer;
                ii = strlen(idProfile);
                ll = strlen(level);
                tt = strlen(type);

                read2.read((char *)&ii,sizeof(int));
                read2.read((char *)&idProfile,ii);
                idProfile[ii]='\0';
                read2.read((char *)&ll,sizeof(int));
                read2.read((char *)&level,ll);
                level[ll]='\0';
                read2.read((char *)&tt,sizeof(int));
                read2.read((char *)&type,tt);
                type[tt]='\0';

            for(int i=0; i<6; i++)
            {
                if(i==0)
                {
                    pointer = strtok(line,"|");
                    strcpy(idUser,pointer);
                    if(del == idUser)
                    {
                        es=1;
                    }
                }
                else if(i==1)
                {
                    pointer = strtok(NULL,"|");
                    strcpy(name,pointer);
                }
                else if(i==2)
                {
                    pointer = strtok(NULL,"|");
                    strcpy(mail,pointer);
                }
                else if(i==3)
                {
                    pointer = strtok(NULL,"|");
                    strcpy(tel,pointer);
                }
                else if(i==4)
                {
                    pointer = strtok(NULL,"|");
                    strcpy(ranking,pointer);
                }
                else if(i==5)
                {
                    pointer = strtok(NULL,"|");
                    //strcpy(idProfile,pointer);

                }
            }
            if(es == 0)
            {
                aux2<<idUser<<'|'<<name<<'|'<<mail<<'|'<<tel<<'|'<<ranking<<'|'<<'\n';

                cout<<" si entra 2"<<endl;
                    aux1.write((char *)&ii,sizeof(int));
                    aux1.write((char *)&idProfile,ii);
                    aux1.write((char *)&ll,sizeof(int));
                    aux1.write((char *)&level,ll);
                    aux1.write((char *)&tt,sizeof(int));
                    aux1.write((char *)&type,tt);

            }
            if(es == 1)
            {
                /////////////////////////////
                while(!read2.eof() && found == false)
                {
                    cout<<"____________DELETED______________"<<endl<<endl;
                    cout<<"ID USER: "<<idUser<<endl;
                    cout<<"NAME: "<<name<<endl;
                    cout<<"MAIL: "<<mail<<endl;
                    cout<<"TEL: "<<tel<<endl;
                    cout<<"RANKING: "<<ranking<<endl;

                    cout<<"Id profile: "<<idProfile<<endl;
                    cout<<"Level: "<<level<<endl;
                    cout<<"Type: "<<type<<endl;
                    cout<<"___________________________________"<<endl;
                    es = 0;
                    //_________________________________________________
                    //________________________________________________
                    found = true;
                }

                if(read.eof())
                {
                    cout<<"File not found"<<endl;
                    break;
                }
                /////////////////////////
                cout<<"_________________________________"<<endl<<endl;
                es = 0;
                found = true;
            }

            read.getline(line,sizeof(line));
            if(read.eof() && found == false )
            {
                cout<<"There isn't a file with this ID."<<endl;
            }
        }
        read.close();
        read2.close();
        aux1.close();
        aux2.close();
        //remove("Profile.txt");
        remove("User.txt");
        //rename("Aux.txt","Profile.txt");
        rename("Aux2.txt","User.txt");
    }
    else if(op == 2)
    {
        string word;
        cout<<"Type the idProfile that you want to find: "<<endl;
        cin>>word;

        int i,l,t;
        ifstream read ("Profile.txt");
        ofstream aux2;
        aux2.open("Aux2.txt",ios::out);
        bool found = false;

        if (!read.good())
        {
            cout<<"\n\n\tFile not found."<<endl;
        }
        else
        {
            while(!read.eof() && found == false)
            {
                read.read((char *)&i,sizeof(int));
                read.read((char *)&idProfile,i);
                idProfile[i]='\0';
                read.read((char *)&l,sizeof(int));
                read.read((char *)&level,l);
                level[l]='\0';
                read.read((char *)&t,sizeof(int));
                read.read((char *)&type,t);
                type[t]='\0';

                if(idProfile != word){
                    i = strlen(idProfile);
                    l = strlen(level);
                    t = strlen(type);

                    aux2.write((char *)&i,sizeof(int));
                    aux2.write((char *)&idProfile,i);
                    aux2.write((char *)&l,sizeof(int));
                    aux2.write((char *)&level,l);
                    aux2.write((char *)&t,sizeof(int));
                    aux2.write((char *)&type,t);
                }

                if(idProfile == word)
                {
                    cout<<"______________FOUND_______________"<<endl;
                    cout<<"Id profile: "<<idProfile<<endl;
                    cout<<"Level: "<<level<<endl;
                    cout<<"Type: "<<type<<endl;
                    cout<<"___________________________________"<<endl;
                    found = true;
                }

                if(read.eof())
                {
                    cout<<"File not found"<<endl;
                    break;
                }

            }
            read.close();
        }
    }
}


En línea

AlbertoBSD
Programador y
Moderador Global
***
Desconectado Desconectado

Mensajes: 3.696


🏴 Libertad!!!!!


Ver Perfil WWW
Re: Problema creando archivo auxiliar para eliminar datos en manejo de datos con c++
« Respuesta #1 en: 12 Septiembre 2017, 18:41 pm »

Bueno realmente no se en que punto te este fallando pero segun [1]

Citar
If the stream is already associated with a file (i.e., it is already open), calling this function fails.


Entonces, en

Código:
else if(op == 2)

Tienes un aux2 que nunca es cerrado, podrias empezar por ahi, cada open debe de tener un close.

Saludos!

[1] http://www.cplusplus.com/reference/fstream/ofstream/open/


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Obtener URL de un archivo de datos MFT - Forensia
Hacking
0x98364 0 1,833 Último mensaje 25 Mayo 2018, 12:29 pm
por 0x98364
ayuda para ordenar datos de un archivo en c
Programación C/C++
jkomarcelino 0 1,415 Último mensaje 3 Agosto 2018, 03:54 am
por jkomarcelino
Problema con manejo de archivo de texto - Consulta
Programación C/C++
Cero++ 0 1,066 Último mensaje 4 Enero 2019, 02:26 am
por Cero++
Protección de Datos anuncia un protocolo de actuación para eliminar vídeos ...
Noticias
wolfbcn 0 719 Último mensaje 25 Enero 2019, 21:50 pm
por wolfbcn
duda para eliminar archivo malicioso
Seguridad
MAVICTOR 3 3,160 Último mensaje 1 Noviembre 2023, 07:36 am
por Howart_drby
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines