I lose 2 records when opening an ofstream C ++

0

I have a serious problem that I can not solve. I do not understand why, when opening an output file I am losing 2 positions of a record vector that I do not touch anywhere.

The problem, I think, I hope you can correct me, is in the module ListingHR (). When compiling, in the output file I'm losing position 0 and 1 of "table", but all the rest of the vector is well preserved ...

  

Output file:

I attach the code:

#include <fstream>
#include <math.h>
#include <iostream.h>
#include <iomanip.h>

using namespace std;

typedef char str20[21];
typedef unsigned short ushort;
const ushort N = 15;

struct t_Archivo{
    float x,
          y;
    str20 direccion;
};
struct t_TblInterna{
    float dist; ///distancia al inicio
    float x,
          y;
    str20 direccion;
};

ushort i = 0;

bool LeerCoord(ifstream &arch, t_Archivo rArch[]);
void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla);
void HojaRuta(t_TblInterna *tabla);
float Distancia(float x2, float x1, float y2, float y1);
void ListadoHR(t_TblInterna tabla[]);
void OrdxBur(t_TblInterna *nomTbl, ushort cardinalidad);
void IntCmb(t_TblInterna &e1, t_TblInterna &e2);



int main(){

    ifstream NLArchivo;

    t_Archivo regArchivo[N];
    t_TblInterna regTbl[N];
    NLArchivo.open("CoordenadasXY.txt");

    while( LeerCoord(NLArchivo, regArchivo) )
        ProcDatosEnTbl(regArchivo,regTbl[i++]);
    cout << "(MAIN 0) " << regTbl[0].direccion << endl;///
    HojaRuta(regTbl);
    cout << "(MAIN 1) " << regTbl[0].direccion << endl;///
    ListadoHR(regTbl);
    NLArchivo.close();
    cout << "\n\n(MAIN 2) " << regTbl[0].direccion << endl;///

    return 0;
}
float Distancia(float x2, float x1, float y2, float y1){
    float dist = sqrt( pow(x2 - x1,2) + pow (y2 - y1,2));
    return dist;
}
void IntCmb(t_TblInterna &e1, t_TblInterna &e2){
    t_TblInterna aux;
    aux = e1;
    e1 = e2;
    e2 = aux;
}
bool LeerCoord(ifstream &arch,t_Archivo rArch[]){
    bool estado = false;
    if(i<N)
    {
        string leido = "";
        arch >> leido;
        rArch[i].x = atof(leido.c_str());
        cout << "Leido: i="<< i << " " << rArch[i].x << " "; ///
        arch >> leido;
        rArch[i].y = atof(leido.c_str());
        cout << rArch[i].y << " "; ///
        arch >>  leido;
        strcpy(rArch[i].direccion, leido.c_str());
        cout << rArch[i].direccion << "." << endl; ///
        estado = true;
    }
    return estado;
}
void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla){
        cout << "estoy en i = " << i << endl; ///
        elemTabla.x = rArch[i-1].x;
        elemTabla.y = rArch[i-1].y;
        elemTabla.dist = 0;
        strcpy(elemTabla.direccion, rArch[i-1].direccion);
        elemTabla.direccion[21] = '
#include <fstream>
#include <math.h>
#include <iostream.h>
#include <iomanip.h>

using namespace std;

typedef char str20[21];
typedef unsigned short ushort;
const ushort N = 15;

struct t_Archivo{
    float x,
          y;
    str20 direccion;
};
struct t_TblInterna{
    float dist; ///distancia al inicio
    float x,
          y;
    str20 direccion;
};

ushort i = 0;

bool LeerCoord(ifstream &arch, t_Archivo rArch[]);
void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla);
void HojaRuta(t_TblInterna *tabla);
float Distancia(float x2, float x1, float y2, float y1);
void ListadoHR(t_TblInterna tabla[]);
void OrdxBur(t_TblInterna *nomTbl, ushort cardinalidad);
void IntCmb(t_TblInterna &e1, t_TblInterna &e2);



int main(){

    ifstream NLArchivo;

    t_Archivo regArchivo[N];
    t_TblInterna regTbl[N];
    NLArchivo.open("CoordenadasXY.txt");

    while( LeerCoord(NLArchivo, regArchivo) )
        ProcDatosEnTbl(regArchivo,regTbl[i++]);
    cout << "(MAIN 0) " << regTbl[0].direccion << endl;///
    HojaRuta(regTbl);
    cout << "(MAIN 1) " << regTbl[0].direccion << endl;///
    ListadoHR(regTbl);
    NLArchivo.close();
    cout << "\n\n(MAIN 2) " << regTbl[0].direccion << endl;///

    return 0;
}
float Distancia(float x2, float x1, float y2, float y1){
    float dist = sqrt( pow(x2 - x1,2) + pow (y2 - y1,2));
    return dist;
}
void IntCmb(t_TblInterna &e1, t_TblInterna &e2){
    t_TblInterna aux;
    aux = e1;
    e1 = e2;
    e2 = aux;
}
bool LeerCoord(ifstream &arch,t_Archivo rArch[]){
    bool estado = false;
    if(i<N)
    {
        string leido = "";
        arch >> leido;
        rArch[i].x = atof(leido.c_str());
        cout << "Leido: i="<< i << " " << rArch[i].x << " "; ///
        arch >> leido;
        rArch[i].y = atof(leido.c_str());
        cout << rArch[i].y << " "; ///
        arch >>  leido;
        strcpy(rArch[i].direccion, leido.c_str());
        cout << rArch[i].direccion << "." << endl; ///
        estado = true;
    }
    return estado;
}
void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla){
        cout << "estoy en i = " << i << endl; ///
        elemTabla.x = rArch[i-1].x;
        elemTabla.y = rArch[i-1].y;
        elemTabla.dist = 0;
        strcpy(elemTabla.direccion, rArch[i-1].direccion);
        elemTabla.direccion[21] = '%pre%';///
        cout << "COPIE /"<< elemTabla.direccion << "/" << endl; ///

        ///cargo todas las direcciones dentro del registro
        ///de tabla interna + X e Y de c/pos
}

void ListadoHR(t_TblInterna tabla[]){

    cout << "\ndoble chk 0 " << tabla[0].direccion << endl; ///
    cout << "\ndoble chk 1 " << tabla[1].direccion << endl; /// hasta aqui perfecto
    ofstream salida ("ListadoHjRt.txt",ios::out);
/// AQUI YA SE LLENO DE BASURA LA POS. 0 Y 1!!!!

    salida << "\t\tListado Hoja de rutas" << endl;
    salida << left << setw(25) << "#Ord.Domicilio" <<
              right<< setw(25) << "Dist. al Destino" << endl;
    for(short ind = 0; ind < N; ind ++){

        salida  << left << setw(4) << (ind+1) << tabla[ind].direccion
                << "(" << setprecision(2) << fixed << tabla[ind].dist
                << ")" << setw(10) << " " <<
                right << setw(10) << tabla[ind].dist << endl;
    }
    salida.close();
    cout << "hasta aca llego" << endl; ///
}

void HojaRuta(t_TblInterna *tabla){
    for(short cont = 1; cont < N ; cont++ ){
        float distanciaAlInicio;
        distanciaAlInicio = Distancia(tabla[cont].x,tabla[0].x,
                                      tabla[cont].y,tabla[0].y);
        tabla[cont].dist = distanciaAlInicio;
    }
    //una vez sacadas todas las distancias, ordeno
    OrdxBur(tabla,N);
    for(short t = 0; t< N ;t++)
    {
        cout << "debug pos " << t << ": " << tabla[t].direccion;
    }
}

void OrdxBur(t_TblInterna *nomTbl, ushort cardinalidad){
    ushort x, y; //contadores
    for(x = 1;x < cardinalidad ; x++){
        for(y = 1; y < cardinalidad - x ; y++)
            if(nomTbl[y].dist > nomTbl[y+1].dist)
                IntCmb(nomTbl[y],nomTbl[y+1]);
    }
}
';/// cout << "COPIE /"<< elemTabla.direccion << "/" << endl; /// ///cargo todas las direcciones dentro del registro ///de tabla interna + X e Y de c/pos } void ListadoHR(t_TblInterna tabla[]){ cout << "\ndoble chk 0 " << tabla[0].direccion << endl; /// cout << "\ndoble chk 1 " << tabla[1].direccion << endl; /// hasta aqui perfecto ofstream salida ("ListadoHjRt.txt",ios::out); /// AQUI YA SE LLENO DE BASURA LA POS. 0 Y 1!!!! salida << "\t\tListado Hoja de rutas" << endl; salida << left << setw(25) << "#Ord.Domicilio" << right<< setw(25) << "Dist. al Destino" << endl; for(short ind = 0; ind < N; ind ++){ salida << left << setw(4) << (ind+1) << tabla[ind].direccion << "(" << setprecision(2) << fixed << tabla[ind].dist << ")" << setw(10) << " " << right << setw(10) << tabla[ind].dist << endl; } salida.close(); cout << "hasta aca llego" << endl; /// } void HojaRuta(t_TblInterna *tabla){ for(short cont = 1; cont < N ; cont++ ){ float distanciaAlInicio; distanciaAlInicio = Distancia(tabla[cont].x,tabla[0].x, tabla[cont].y,tabla[0].y); tabla[cont].dist = distanciaAlInicio; } //una vez sacadas todas las distancias, ordeno OrdxBur(tabla,N); for(short t = 0; t< N ;t++) { cout << "debug pos " << t << ": " << tabla[t].direccion; } } void OrdxBur(t_TblInterna *nomTbl, ushort cardinalidad){ ushort x, y; //contadores for(x = 1;x < cardinalidad ; x++){ for(y = 1; y < cardinalidad - x ; y++) if(nomTbl[y].dist > nomTbl[y+1].dist) IntCmb(nomTbl[y],nomTbl[y+1]); } }
    
asked by Juan 07.08.2018 в 07:57
source

1 answer

2

Your code is complicated to follow and complex in excess, it is normal that you do not find the error. In this type of problems, in general, the problem is not usually in the data display (in your case ListadoHR ) but in the reading (in your case LeerCord ) or in the saved. Your code is not an exception: you are processing the data you are reading wrong, in the function ProcDatosEnTbl :

void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla){
    cout << "estoy en i = " << i << endl; ///
    elemTabla.x = rArch[i-1].x; // ¿Por qué restar una posición
    elemTabla.y = rArch[i-1].y; // en este array?
    elemTabla.dist = 0;
    strcpy(elemTabla.direccion, rArch[i-1].direccion);
    elemTabla.direccion[21] = '
void ProcDatosEnTbl(t_Archivo *rArch, t_TblInterna &elemTabla){
    cout << "estoy en i = " << i << endl; ///
    elemTabla.x = rArch[i-1].x; // ¿Por qué restar una posición
    elemTabla.y = rArch[i-1].y; // en este array?
    elemTabla.dist = 0;
    strcpy(elemTabla.direccion, rArch[i-1].direccion);
    elemTabla.direccion[21] = '%pre%';///
    cout << "COPIE /"<< elemTabla.direccion << "/" << endl; ///
}
';/// cout << "COPIE /"<< elemTabla.direccion << "/" << endl; /// }

When the global variable i is 0 you access the record -1 and consequently you fill in the table with unexpected values.

Other things to consider.

  • The headers <iostream.h> e <iomanip.h> do not exist, the standard headers of C ++ have no extension, surely you wanted to include <iostream> e <iomanip> .
  • The header <math.h> is C, <cmath> is the adapted version C ++ and is the one you should use, read this thread to know more details.
  • Avoid using using namespace std , and if you use it do it in the smallest possible scope, not in the global scope, read this thread to know more details.
  • Avoid using global variables, read this thread to learn more details, you can also read this thread for additional details.
  • Abandons the use of 1 training, C ++ offers tools that are much more flexible, less prone to errors, and easier to use as std::string and std::list .
  • Abandons the use of raw pointers, C ++ offers much safer utilities such as references.
  • Do not use std::endl unless it is strictly necessary, read this thread to learn more details.
  • Also known as arrays or in English arrays .
  • answered by 07.08.2018 в 08:29