Borland 5.5 C++ Ifstream 0xC0000005

0

I am doing a school project and my teacher wants it to compile in Borland 5.5. The code works quite fine in GNU GCC compiler, but here it is a mess. I tracked the error to the use of ifstream (I use it to read a .txt file), but what confuses me the most is that even instantiate ifstream crashes. I will post the entyre code and two pieces that I was testing with Ifstream.

#include <iostream>
#include <fstream>
using namespace std;

ifstream arch_paises;

int main () {
  cout << "End..." << endl;
  return 0;
}

The last line (print "End") doesnt even execute. But... if I change the ifstream position..

#include <iostream>
#include <fstream>

using namespace std;

int main () {
  ifstream arch_paises;
  cout << "End..." << endl;
  return 0;
}

The "end" is printed. Anyway the "returned -1073741819" is still there.

Here I post the entyre code of the project, as I said with the GNU GCC compiler it works perfectly. It read some data from text files and makes some calculations about the pandemic.

//------------------------------------------------------------------------------
// Librerias
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string.h>

using namespace std;

//------------------------------------------------------------------------------
// Parametros de configuracion
const short MAX_PAIS = 200;
#define n_paises "Paises.txt"
#define n_partes "ParteDiario.txt"

//------------------------------------------------------------------------------
// Posibles titulos
const string titulo[] = {"Hisopados", "Infectados" , "Recuperados", "Fallecidos"};

//------------------------------------------------------------------------------
// Estructura con informacion mundial
struct tsGlobal{
    short cp              ;                   //  Cantidad de paises
    long     poblacion    ;                   //  Poblacion mundial
    long     totales_globales[5];             //  Hisopados(1) , Infectados(2) , Recuperados(3) , Fallecidos(4)
};

//------------------------------------------------------------------------------
// Estructura utilizada para los calculos
struct datos_paises{
   char  pais[20];
   char  cont[10];
   unsigned short habs           ;
   unsigned short mensual[5][13] ; // [Hisopados(1) , Infectados(2) , Recuperados(3) , Fallecidos(4)] [mes]
   unsigned short totales[5]     ; //  Hisopados(1) , Infectados(2) , Recuperados(3) , Fallecidos(4)
};

//------------------------------------------------------------------------------
// Estructura utilizada para la lectura de los partes diarios
struct tsParDia{
    char pais[20];
    unsigned short mes;
    unsigned short dia;
    unsigned short  ch;
    unsigned short  ci;
    unsigned short  cf;
    unsigned short  cr;
};

//------------------------------------------------------------------------------
// Prototipos de modulos
void  Abrir_archivo(ifstream &paises, ifstream &partes);
void  ProcPaises(tsGlobal &i, datos_paises* tsC, ifstream &paises);
bool  LeerPaises(datos_paises* r, short index,ifstream &paises);
void  ProcParteDiarios( datos_paises* ps,tsGlobal &tsg, ifstream &partes, tsParDia tsPart);
bool  LeerParte(ifstream &partes,tsParDia &tsPart);
void  Ordenar(datos_paises* ps,int cantidad,int criterio);
void  IntCmb(datos_paises &elem1,datos_paises &elem2);
void  Listado(tsGlobal tsg,datos_paises* ps);
void  Generar_contenido_archivo(datos_paises* estructura,short tipo,tsGlobal tsg,datos_paises* ps);
void  archivo_formato(short tipo,ofstream &outfile,datos_paises* ps);
void  archivo_body(short cantidadPaises,ofstream &outfile,short tipo,datos_paises* ps);
void  archivo_footer(short tipo,ofstream &outfile, datos_paises* ps,tsGlobal tsg);
void  Cerrar_archivo(ifstream &paises, ifstream &partes);

//+=============================================================================
// Bloque principal
//
int main() {
    /*Declaracion de estructuras*/
    datos_paises ps [MAX_PAIS];
    tsParDia     tsPartes;
    tsGlobal     tsGlb;

    /*Archivos*/
    ifstream arch_paises;
    ifstream arch_partes;

    /*Abro los archivos*/
    Abrir_archivo(arch_paises, arch_partes);
    /*Proceso la informacion del archivo de paises*/
    ProcPaises(tsGlb, ps, arch_paises);
    /*Proceso la informacion del archivo de partes diarios*/
    ProcParteDiarios(ps, tsGlb, arch_partes, tsPartes);
    /*Genero el listado*/
    Listado(tsGlb,ps);
    /*Cierro los archivos*/
    Cerrar_archivo(arch_paises, arch_partes);

    return 0;
}

//+=============================================================================
// Abro los archivos para lectura
//
void Abrir_archivo(ifstream &paises, ifstream &partes){
    paises.open(n_paises);
    partes.open(n_partes);
}

//+=============================================================================
// Proceso la linea y la almaceno en la posicion indicada de la struct
//
void ProcPaises(tsGlobal &i, datos_paises* tsC, ifstream &paises){
    i.cp = 0;
    while(LeerPaises(tsC, i.cp, paises)){
        i.poblacion += tsC[i.cp].habs;
        i.cp++;
    }
}

//+=============================================================================
// Leo el archivo de paises y lo almaceno en la estructura interna
//
bool LeerPaises(datos_paises* r, short cp,ifstream &paises) {
    paises.get(r[cp].pais,21, ' ');
    paises >> ws;
    paises.get(r[cp].cont,11, ' ');
    paises >> ws;
    paises >> r[cp].habs;
    paises.ignore();

    return paises.good();
} // Leer

//+=============================================================================
// Proceso la linea del parte y la almaceno en la posicion indicada de la struct
//
void ProcParteDiarios( datos_paises* ps,tsGlobal &tsg, ifstream &partes, tsParDia tsPart){
    while(LeerParte(partes, tsPart)){
       for(int c = 0; c < tsg.cp; c ++){
            if(!strcmp(tsPart.pais,ps[c].pais)){
            ps[c].mensual[1][tsPart.mes]+= tsPart.ch;
            ps[c].totales[1]            += tsPart.ch;
            tsg.totales_globales[1]     += tsPart.ch;
            ps[c].mensual[2][tsPart.mes]+= tsPart.ci;
            ps[c].totales[2]            += tsPart.ci;
            tsg.totales_globales[2]     += tsPart.ci;
            ps[c].mensual[3][tsPart.mes]+= tsPart.cr;
            ps[c].totales[3]            += tsPart.cr;
            tsg.totales_globales[3]     += tsPart.cr;
            ps[c].mensual[4][tsPart.mes]+= tsPart.cf;
            ps[c].totales[4]            += tsPart.cf;
            tsg.totales_globales[4]     += tsPart.cf;
        }
       }
    }
}

//+=============================================================================
// Leo el archivo de partes y lo almaceno en la estructura interna
//
bool LeerParte(ifstream &partes, tsParDia &tsPart) {
    /*Nombre del pais*/
    partes.get(tsPart.pais,21, ' ');
    /*Descargo el mes*/
    partes >> tsPart.mes;
    /*Descargo el dia*/
    partes >> tsPart.dia;
    /*Descargo hisopados*/
    partes >> tsPart.ch;
    /*Descargo infectados*/
    partes >> tsPart.ci;
    /*Descargo fallecidos*/
    partes >> tsPart.cr;
    /*Descargo recuperados*/
    partes >> tsPart.cf;
    partes.ignore();
    return partes.good();
} // Leer

//+=============================================================================
// Ordena el struct en funcion de un parametro especifico
//
void Ordenar(datos_paises* ps,int cantidad,int criterio){
    bool cambios = true;
    char grupos=cantidad/2;
    while(grupos>1){
        grupos = grupos/2;
        cambios = true;
        while(cambios){
            cambios = false;
            for (char i=0;i+grupos<cantidad;i++){
                if(ps[i].totales[criterio] < ps[i+grupos].totales[criterio])
                {
                    cambios=true;
                    IntCmb(ps[i],ps[i+grupos]);
                }
            }
        }
    }
}

//+=============================================================================
// Intercambia posiciones del Struct
//
void IntCmb(datos_paises &elem1,datos_paises &elem2){
    datos_paises aux;
    aux   = elem1;
    elem1 = elem2;
    elem2 = aux;
}

//+=============================================================================
// Genero el listado de los distintos topicos y guardo en archivos
//
void  Listado(tsGlobal tsg,datos_paises* ps){
    for(int i = 1; i < 5; i++){
        Ordenar(ps,tsg.cp,i);      // Ordeno la estructura en funcion del parametro dado
        Generar_contenido_archivo(ps,i,tsg, ps);
    }
}

//+=============================================================================
// Genero el contenido de los archivos
//
void Generar_contenido_archivo(datos_paises* estructura,short tipo,tsGlobal tsg,datos_paises* ps){
    ofstream outfile;
    string nombre = "Listado" + titulo[tipo-1] + ".txt";
    outfile.open(nombre.c_str());
    archivo_formato(tipo,outfile,ps);
    archivo_body(tsg.cp,outfile,tipo,ps);
    archivo_footer(tipo,outfile,ps,tsg);
    outfile.close();
}

//+=============================================================================
// Primera parte del archivo en donde plasmo el formato solicitado por el docente
//
void  archivo_formato(short tipo,ofstream &outfile,datos_paises* ps){
   /*Titulo*/
   outfile << setw(100) << endl;
   outfile << setw(77)  << "Listado de " + titulo[tipo-1] << endl;
   /*Linea 1*/
   outfile << left << setw(10)<< "Nro.";
   outfile << left << setw(20)<< "Nombre";
   outfile << left << setw(10)<< "Cant.Hab";
   /*Linea2*/
   string auxiliar = "---------- Cantidades de "  + titulo[tipo-1]  + " por mes ----------";
   outfile << left  << setw(47)<< auxiliar;
   outfile << right << setw(6) << "Cant.";
   outfile << right << setw(15)<< "Porcentajes" <<endl;
   outfile << left  << setw(10)<< "Ord.";
   outfile << left  << setw(20)<< "Pais";
   outfile << left  << setw(10)<< "";
   outfile << left  << setw(8) << "Ene";
   outfile << left  << setw(8) << "Feb";
   outfile << left  << setw(8) << "Mar";
   outfile << left  << setw(8) << "Abr";
   outfile << left  << setw(8) << "May";
   outfile << left  << setw(8) << "Jun";
   outfile << left  << setw(7) << "Jul";
   outfile << left  << setw(10)<< "Tot." << endl;
}

//+=============================================================================
// Cuerpo del archivo... Coloco la info por pais
//
void  archivo_body(short cantidadPaises,ofstream &outfile,short tipo,datos_paises* ps){
   for(int i = 0; i < cantidadPaises ; i ++){
       outfile << left  << setw(10) << i+1;
       outfile << left  << setw(20) << ps[i].pais;
       outfile << left  << setw(10) << ps[i].habs;
       outfile << left  << setw(8)  << ps[i].mensual[tipo][1];
       outfile << left  << setw(8)  << ps[i].mensual[tipo][2];
       outfile << left  << setw(8)  << ps[i].mensual[tipo][3];
       outfile << left  << setw(8)  << ps[i].mensual[tipo][4];
       outfile << left  << setw(8)  << ps[i].mensual[tipo][5];
       outfile << left  << setw(8)  << ps[i].mensual[tipo][6];
       outfile << left  << setw(7)  << ps[i].mensual[tipo][7];
       outfile << left  << setw(10) << ps[i].totales[tipo];
       double porcentaje = ((double)ps[i].totales[tipo] / (double)ps[i].habs) * 100;
       char aux[50];
       sprintf(aux,"%.2f", porcentaje);
       outfile << right << setw(6)  << aux << "%" << endl;
   }
}

//+=============================================================================
// Coloco el footer del archivo
//
void archivo_footer(short tipo,ofstream &outfile, datos_paises* ps,tsGlobal tsg){
   string foot1 = "Cantidad total de " + titulo[tipo-1] + " a la fecha actual:";
   outfile << endl << endl << left  << foot1 << tsg.totales_globales[tipo] << endl;

   double porcentaje = ((double) tsg.totales_globales[tipo] / (double)tsg.poblacion) * 100;
   char aux[50];
   sprintf(aux,"%.2f", porcentaje);

   string foot2 = "Porcentaje de " + titulo[tipo-1];
   outfile << left  << setfill('.') << setw(foot1.length()-1) << foot2;
   outfile << right << ":" << aux << "%" ;
}


//+=============================================================================
// Cerramos los archivos de lectura
//
void  Cerrar_archivo(ifstream &paises, ifstream &partes){
    paises.close();
    partes.close();
}

The code compiles without errors or warnings but it doesnt runs properly/entirely with Boorland.

Thanks.

c++
ifstream
borland-c++
asked on Stack Overflow Sep 25, 2020 by Nicolás Fernández Sanz • edited Sep 25, 2020 by Nicolás Fernández Sanz

1 Answer

3

0xC0000005 is Windows exception code for memory access violation. One example would be writing to NULL address.

ifstream is part of C++ standard library, and each compiler provides its own implementation of it. Borland C++ 5.5 is 20 years old and was not designed to be used with OS newer then Windows 2000.

Since according to your question the error occurs by simply instantiating ifstream, I believe the problem is somewhere within Borland's implementation of ifstream that is simply incompatible with the newer OS you are running.

You can try to run the resulting EXE in compatibility mode (if that is still available on your version of windows).

Another option may be avoiding ifstream and using the C API instead fopen, fread or using WIN32 API CreateFile and ReadFile.

Unless of course you can convince your teacher to switch compilers.

answered on Stack Overflow Sep 25, 2020 by Lev M.

User contributions licensed under CC BY-SA 3.0