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]);
}
}