# Problem with fstream library

**URL:** <https://root-forum.cern.ch/t/problem-with-fstream-library/33846>\
**Category:** Newbie\
**Created:** [May 1, 2019, 12:50pm UTC](https://root-forum.cern.ch/t/problem-with-fstream-library/33846 "2019-05-01T12:50:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnyBoy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/johnyboy/32/19741_2.png) [@JohnyBoy](https://root-forum.cern.ch/u/JohnyBoy)\
**Post date:** [May 1, 2019, 12:50pm UTC](https://root-forum.cern.ch/t/problem-with-fstream-library/33846/1 "2019-05-01T12:50:32Z")

</div>

Hello everyone.

I have a problem with the fstream library in C++ when I try to use it in a ROOT macro. This is what I’m trying to do.  
I have a file (call it F1) with 4 data columns. I 'm trying to read those columns in order to run a simulation. The problem is that ROOT doesn’t seem to find the fstream library and is therefore unable to run my simulation. Any advice? I am leaving my code below.  
Thanks in advance.

```auto
void Integrated_results()
{
    int Nevents = 1000000;
    std::vector <long double> Energy; // vector to store a nucleus energy
    std::vector <long double> Charge; // vector to store a nucleus charge
    std::vector <long double> Mass; // vector to store a nucleus mass
    std::vector <long double> Redshift; // vector to store a nucleus redshift
    long double Ee;
    long double Q;
    long double M;
    long double Z;

    long double c = 3.06562/pow(10, 7); // Lights speed in MPc/yr
    float Bamp = 20; // Turbulent magnetic field amplitude in nG
    float Lcoh = 0.25; // Coherence lenght in MPc
    //int zz = 1; // Electric Charge of the particle //Now rendered useless due to a code update
    float Ec0; // Critical Energy
    long double Ho = 7.2/pow(10., 11); // Hubble's constant
    long double Omegam = 0.3; // Matter density of the Universe
    long double Omegade = 1.-Omegam; // Dark energy density of the Universe
    float ai = 0.9, al = 0.23, rmk = 5./3.; // Parameters for the Kolmogorov B spectrum
    int m = 1; // Index that considers MHD energy losses
    long double ld, h; // h represents both the step in time and space
    long double dz = -0.0000001; // step in redshift
    long double dx;
    //int i=0;
    long double y[7]; // Components 0, 1 & 2 represent the speed of the
                        // particle. Components 3, 4 & 5 represent the position
                        // Given that particles are always in the ultrarelativistic
                        // limit, their speed will be assumed
                        // to equal that of light. c=1
    long double a;

    
    std::string line;
    std::ifstream g, l;
    std::ofstream f;

    f.open("/home/oscar/Desktop/Propagation in both things/Density Enhancement/Final conditions 2546C.txt");
    g.open("/home/oscar/Desktop/Integrated results/Heaviest nucleus 2.txt");
    l.open("/home/oscar/Desktop/Integrated results/Heaviest nucleus 2.txt");
    
    //mypros << "Energy (EeV)" <<" "<<"Dist (Mpc)" <<" "<<"cospos"<<" "<<"Electric Charge" <<" "<<"Mass"<<" "<<"Zini"<< '\n';

    for(int i=0; i<Nevents ; i++)
    {std::cout <<i<<'\n';

        while(true) 
        {
            std::getline(l, line);
            
            if (line.empty()) 
            {
                std::getline(l, line); 
                break;
            } // When there is an empty line the event has ended.

            g >> Ee >> Z >> Q >> M; // Reads values from the data file
            if(M>0 && Q>0)
            {
                if (Redshift.size() != 0)
                {
                    if (Z !=Redshift[Redshift.size()-1])
                    {
                        Energy.push_back (Ee); // Stores the corresponding value on the corresponding vector
                        Redshift.push_back(Z);
                        Charge.push_back (Q);
                        Mass.push_back (M);
                    }
                }
                else
                {
                    Energy.push_back (Ee); // Stores the corresponding value on the corresponding vector
                    Redshift.push_back(Z);
                    Charge.push_back (Q);
                    Mass.push_back (M);
                }
            }
        }
        
        Initiatearray (y, Redshift[0]); // Lays out the initial
                                                //conditions for the UHECRs propagation
        for(unsigned int j=0; j<Redshift.size(); j++)
        {
            long double Zo, Zf;
            
            if((j) >= (Redshift.size()-1))      
            {
                Zo = Redshift[j];
                Zf = 0;
            }
            else // For every other case,
            {
                Zo = Redshift[j];
                Zf = Redshift[j+1];
            }
            
            Ee = Energy[j];
            a = 1./(1+y[6]);
            Ec0 = 0.935*Bamp*Lcoh*Charge[j]*pow(10,18);
                    
            ld = (Lcoh/(1.+y[6]))*(4.*(Ee/Ec0)*(Ee/Ec0)*pow(1.+y[6], 2*m)*pow(a,2) + \
                ai*Ee/Ec0*pow(1.+y[6], m)*a + \
                al*pow(Ee/Ec0 , 2.-rmk)*pow(a, 2.-rmk)*pow(1.+y[6], m*(2.-rmk)));
            h = ld/10.;
            dx = -c*dz/(Ho*sqrt(Omegam*pow(1+y[6], 3) + Omegade));
            //std::cout << h<<" "<<dx<<'\n';
            EvolveParticle(y, dx, h, ld, dz, Zo, Zf);
        }
        
        long double dist;
        long double cospos;
        dist = sqrt(y[3]*y[3]+y[4]*y[4]+y[5]*y[5]);
        cospos = (y[0]*y[3]+y[1]*y[4]+y[2]*y[5])/dist;
        f << (Ee/(1.+Redshift[Redshift.size()-1]))/pow(10,18) << " " <<dist<<" " <<cospos<< " " <<Charge[Charge.size()-1]\
               <<" "<<Mass[Mass.size()-1]<< " " <<Redshift[0]<< '\n';

        Energy.clear(); // Empties all vectors in order for them to be ready to read the next event
        Charge.clear();
        Mass.clear();
        Redshift.clear();
    }

    f.close();   
    g.close(); l.close();
}

```

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [May 2, 2019, 7:32am UTC](https://root-forum.cern.ch/t/problem-with-fstream-library/33846/2 "2019-05-02T07:32:01Z")

</div>

Does the problem go away if you include the header in the beginning of the file (i.e. `#include <fstream>`)?

BTW, you probably want to use plain `double` instead of `long double`, as the latter is a lot slower and you probably don’t need the extra precision it gives.

---

<div class="post-metadata">

**Author:** ![JohnyBoy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/johnyboy/32/19741_2.png) [@JohnyBoy](https://root-forum.cern.ch/u/JohnyBoy)\
**Post date:** [May 4, 2019, 1:38pm UTC](https://root-forum.cern.ch/t/problem-with-fstream-library/33846/3 "2019-05-04T13:38:33Z")

</div>

Hello.  
Yes. It was that. Thanks.
