Note that there are some explanatory texts on larger screens.

plurals
  1. POC++, Problems with Binary Files in sequence mode
    primarykey
    data
    text
    <p>I have a quite difficult problem with binary files. I have been asked to make a program that stores information in files, but in Sequential mode. As I am not allowed to modify things in sequential mode directly, I made a function that first reads the file until I found the correct registry, while copying the other registries into an auxiliary file. when I finished modifying what I needed, I copy it to the auxiliary file and resume copying. When finished, I copy everything from the auxiliary file to the original one. I have made this following various examples of binary files, yet my program does some weird things. It begins eating all the information I wrote, leaving only the last entry (something like infinitely truncating), And even worse than that, there is a part (labeled the "pesky part") that starts an infinite loop that has no sense (it's as if the file had infinite size) I have chacked the logic like a thousand times and I don't seem to find any mistake, I don't know if you can help me, if I am missing something important.</p> <p>Here are the classes I'm using</p> <pre><code>class Cliente{ public: int numCuenta; char dni[10]; char nombre[40]; }; class Cuenta{ private: int numCuenta; double monto; int numDuenhos; public: const static double MONTO_MIN = 100.0; Cuenta(){ numCuenta = 0; monto = 0; numDuenhos = 0; } int getnumCuenta(){ return numCuenta; } void setnumCuenta(int numCuenta){ this-&gt;numCuenta= numCuenta; } int getnumDuenhos(){ return numDuenhos; } void setnumDuenhos(int numDuenhos){ this-&gt;numDuenhos= numDuenhos; } double getMonto(){ return monto; } void setMonto(double monto){ this-&gt;monto = monto; } }; </code></pre> <p>and the problematic function</p> <pre><code> void modificarCuenta() { Cuenta aux; Cliente c; ifstream rep_cuentas("cuentas.bin"); ofstream buf_cuentas("cuentas_rep.bin",ios::out | ios::trunc | ios::binary); if(!rep_cuentas) { cout &lt;&lt;endl &lt;&lt; "Error al leer fila principal"; } else if(!buf_cuentas) { cout &lt;&lt; endl &lt;&lt; "Error al abrir el archivo buffer"; } else { cout &lt;&lt;endl &lt;&lt; "Ingrese el numero de cuenta a modificar: "; //id of entry int num_cuenta; cin &gt;&gt; num_cuenta; ifstream rep_clientes("clientes.bin"); ofstream buf_clientes("cilentes_rep.bin",ios::out | ios::trunc | ios::binary); //este archivo es necesario, por eso termina si no lo lee if (!rep_clientes) { cerr &lt;&lt; "Error al Abrir el Archivo de Clientes" &lt;&lt; endl; return; } rep_cuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); while(!rep_cuentas.eof()){ if(aux.getnumCuenta() == num_cuenta){ rep_clientes.read(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); while(!rep_clientes.eof()){ if(c.numCuenta == num_cuenta){ cout &lt;&lt; "DNI del Cliente: " &lt;&lt; c.dni &lt;&lt; endl; //old dni cout &lt;&lt; "Nombre del Cliente: " &lt;&lt; c.nombre &lt;&lt; endl; // old name cout &lt;&lt; "Modificar estos datos? (1 para confirmar): "; int opc; cin &gt;&gt; opc; if (opc == 1){ c.numCuenta = aux.getnumCuenta(); cout &lt;&lt; endl &lt;&lt; "Ingrese nuevo DNI: "; //new dni cin &gt;&gt; c.dni; cout &lt;&lt; endl &lt;&lt; "Ingrese nuevo Nombre: "; //new name cin &gt;&gt; c.nombre; } } buf_clientes.write(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); rep_clientes.read(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); } int num = aux.getnumDuenhos(); while(true){ cout &lt;&lt; endl &lt;&lt; "Desea ingresar mas duenhos? (1 para confirmar): "; //appending new user? int op; cin &gt;&gt; op; if (op == 1){ c.numCuenta = aux.getnumCuenta(); cout &lt;&lt; endl &lt;&lt; "Ingrese nuevo DNI: "; //new dni cin &gt;&gt; c.dni; cout &lt;&lt; endl &lt;&lt; "Ingrese nuevo Nombre: "; //new name cin &gt;&gt; c.nombre; num++; buf_clientes.write(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); } else{ aux.setnumDuenhos(num); break; } } } buf_cuentas.write(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); rep_cuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); } rep_clientes.close(); buf_clientes.close(); } rep_cuentas.close(); buf_cuentas.close(); ofstream rcuentas("cuentas.bin",ios::out | ios::trunc| ios::binary); ifstream bcuentas("cuentas_rep.bin"); if(!rcuentas) { cout &lt;&lt; endl &lt;&lt; "Error al abrir la fila principal"; } else if(!bcuentas) { cout &lt;&lt; endl &lt;&lt; "Error al abrir el archivo buffer"; } else{ bcuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); while(!bcuentas.eof()){ rcuentas.write(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); bcuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); } rcuentas.close(); bcuentas.close(); ofstream rclientes("clientes.bin",ios::out | ios::trunc | ios::binary); ifstream bclientes("clientes_rep.bin"); bclientes.read(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); //pesky part while(!bclientes.eof()){ rclientes.write(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); bclientes.read(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); } //end of pesky part bclientes.close(); rclientes.close(); cout &lt;&lt; endl &lt;&lt; "Modificacion Realizada con Exito" &lt;&lt; endl; //confirmation text } } </code></pre> <p>In case you need it, this is the function to write a new entry, it works perfectly fine:</p> <pre><code>void crearCuenta(){ Cuenta aux; aux.setnumCuenta(0); ifstream rcuentas("cuentas.bin"); if(!rcuentas){ cout&lt;&lt; endl &lt;&lt;"Primer uso del Sistema detectado, generando numero de cuenta inicial" &lt;&lt;endl; } else { rcuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); while(!rcuentas.eof()){ rcuentas.read(reinterpret_cast&lt;char *&gt;(&amp;aux),sizeof(Cuenta)); } rcuentas.close(); } Cuenta cu; cu.setnumCuenta(aux.getnumCuenta() + 1); int num_duenhos = 0; ofstream a_clientes("clientes.bin",ios::app |ios::binary); while(true){ char dni[10]; cout &lt;&lt; "Ingrese el DNI del Duenho: "; //new dni Cliente c; cin &gt;&gt; c.dni; cout &lt;&lt; "Ingrese el Nombre del Duenho: "; //new name cin &gt;&gt; c.nombre; c.numCuenta = cu.getnumCuenta(); num_duenhos++; a_clientes.write(reinterpret_cast&lt;char *&gt;(&amp;c),sizeof(Cliente)); cout &lt;&lt; "Desea ingresar otro duenho? (escriba 1 para confirmar): "; //another entry? int val; cin &gt;&gt; val; if (val != 1) break; } cu.setnumDuenhos(num_duenhos); while(true){ double monto; cout &lt;&lt; endl; cout &lt;&lt; "Ingrese el monto con el cual iniciara la cuenta:"; //numerical value (greater than 100) cin &gt;&gt; monto; if (monto &lt; Cuenta:: MONTO_MIN){ cout &lt;&lt; "Debe ingresar un valor mayor a " &lt;&lt; Cuenta::MONTO_MIN &lt;&lt; endl; } else{ cu.setMonto(monto - monto * 0.005); break; } } ofstream acuentas("cuentas.bin",ios::app| ios::binary); if(!acuentas){ cout&lt;&lt; endl &lt;&lt;"ERROR en la fila secuencial" &lt;&lt;endl; } else{ acuentas.write(reinterpret_cast&lt;char *&gt;(&amp;cu),sizeof(Cuenta)); acuentas.close(); } cout &lt;&lt; "Cuenta Guardada Satisfactoriamente con número: " &lt;&lt; cu.getnumCuenta() &lt;&lt; endl; //confirmation text } </code></pre> <p>Don't mind the spanish text, it's just for user communication. Any help will be highly appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload