how to write, read and rewrite in a .txt file with c++

2.2k Views Asked by At

i'm a student and i have to create a program and an inform about the use of the program, so ii has to, if the file doesn´t exist, create it; if it exist, read it and save the variable, then it has to increase their value if it meets specific conditions, finally the program has to rewrite the file with the new values, save, and close it. i made a code but it works bad, i dont know where is the mistake. help me please.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()

{
int varAux1,varAux2,varAux3,varAux4;
int a;
int var1,var2,var3,var4;


ifstream archivoLectura;
archivoLectura.open("informeuso.txt");


    if (archivoLectura.fail())
    {
        ofstream archivoLectura;
        archivoLectura.open("informeuso.txt");

            archivoLectura<<"linea 1: "<<0<<"\n";
            archivoLectura<<"linea 2: "<<0<<"\n";
            archivoLectura<<"linea 3: "<<0<<"\n";
            archivoLectura<<"linea 4: "<<0<<"\n";

        archivoLectura.close();
        archivoLectura.open("informeuso.txt");

    }


    archivoLectura>>var1;
    archivoLectura>>var2;
    archivoLectura>>var3;
    archivoLectura>>var4;


    varAux1 = var1;
    varAux2 = var2;
    varAux3 = var3;
    varAux4 = var4;


archivoLectura.close();


cout<<"hola"<<endl;
cout<<"programa prueba"<<endl;
cout<<"enter value from 0 to 9"<<endl;
cout<<"a: ";
cin >> a;



if (a>0 && a <3)

{
    var1 = varAux1 + 1;
}
else if(a>2 && a <6)

{
    var2 = varAux2 + 1;

}
else if (a>5 && a<8)
{
    var3 = varAux3 +1;

}
else
{
    var4 = varAux4 + 1;
}


ofstream archivoEscritura;
archivoEscritura.open("informeuso.txt");

    archivoEscritura<<"line 1-2: "<<var1<<"\n";
    archivoEscritura<<"line 3-5: "<<var2<<"\n";
    archivoEscritura<<"line 6-7: "<<var3<<"\n";
    archivoEscritura<<"line other: "<<var4<<"\n";

archivoEscritura.close();

system("pause");
return 0;
}
2

There are 2 best solutions below

8
On

I see two different variables with exactly the same name, but different types, and different scopes.

Do you think this is a little confusing??

ifstream archivoLectura;

ofstream archivoLectura;


Following your code:

Step 1:

ifstream archivoLectura;
archivoLectura.open("informeuso.txt");  // This might fail, and you check for archivoLectura.fail()

Step 2:

archivoLectura>>var1;  // trying to read from the ifstream, which failed to open.
archivoLectura>>var2;  // The ofstream (which was opened successfully) has gone out of scope.
archivoLectura>>var3;
archivoLectura>>var4;

Here you are trying to read from a file that we know failed to open.
That seems like a big problem to me.

0
On

Check this .... I think that's the solution

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()

{
int varAux1,varAux2,varAux3,varAux4;
int a;
int var1,var2,var3,var4;


fstream archivoLectura;
archivoLectura.open("informeuso.txt" , ios::in);


    if (archivoLectura.fail())
    {
            archivoLectura.open("informeuso.txt" , ios::out);
            archivoLectura<<0<<" ";
            archivoLectura<<0<<" ";
            archivoLectura<<0<<" ";
            archivoLectura<<0<<" ";      
            archivoLectura.close();
            archivoLectura.open("informeuso.txt" , ios::in);
    }






    archivoLectura>>var1;
    archivoLectura>>var2;
    archivoLectura>>var3;
    archivoLectura>>var4;
    cout<<var1<<var2<<var3<<var4;

    varAux1 = var1;
    varAux2 = var2;
    varAux3 = var3;
    varAux4 = var4;


archivoLectura.close();


cout<<"hola"<<endl;
cout<<"programa prueba"<<endl;
cout<<"enter value from 0 to 9"<<endl;
cout<<"a: ";
cin >> a;



if (a>0 && a <3)

{
    var1 = varAux1 + 1;
}
else if(a>2 && a <6)

{
    var2 = varAux2 + 1;

}
else if (a>5 && a<8)
{
    var3 = varAux3 +1;

}
else
{
    var4 = varAux4 + 1;
}


fstream archivoEscritura;
archivoEscritura.open("informeuso.txt" , ios::out);

    archivoEscritura<<var1<<" ";
    archivoEscritura<<var2<<" ";
    archivoEscritura<<var3<<" ";
    archivoEscritura<<var4<<" ";

archivoEscritura.close();

system("pause");
return 0;
}