File I/O with Xcode 3.0 for Mac

I have an issue getting my code to work. The following code shown is the function I am using to calculate the RMS of data I have collected from a CFD simulation. I want to read from a file called dynamics.txt that is on my desktop. Below the code I included a couple lines from the text file. The columns of the file are as follows: time, x force, y force, z force, x moment, y moment, z moment, pitch, roll, yaw. I am only concerned with the time and x force... hence the other values being put into the variable g. You may need to make the window larger for the text in the code and data file to look neat and readable.

Any help would be great, thanks!




double RMS()
{

int i(0);
double time(0), drag(0), g, rms;
long double sumsq(0);
bool collect = false;
ifstream fin;

fin.open("~/Desktop/dynamics.txt");

if(fin.fail())
{
cout << "File failed to open" << endl;
exit(1);
}
else
{


while(!collect)
{
fin >> time >> drag >> g >> g >> g >> g >> g >> g >> g >> g; //collect time, drag, and other garbage

if (time >= 0.244611798839E+00) //want time that is the closest larger value
{
collect = true;
}

if (collect)
{
while(!fin.eof())
{
sumsq = drag*drag + sumsq;
i++;
fin >> time >> drag >> g >> g >> g >> g >> g >> g >> g >> g;
}
}
}
}//end if-else

rms= sqrt(sumsq/i);

fin.close(); //CLOSE FILE

return rms;

}





EXAMPLE LINES FROM TEXT FILE:


0.244610891327E+00 0.460960E+04 0.288081E+02 0.720051E+02 -0.618606E-01 -0.125136E+03 0.537281E+02 0.000000E+00 0.000000E+00 0.000000E+00
0.244611345083E+00 0.461020E+04 0.290706E+02 0.720907E+02 -0.618698E-01 -0.125303E+03 0.541948E+02 0.000000E+00 0.000000E+00 0.000000E+00
0.244611798839E+00 0.461080E+04 0.293327E+02 0.721764E+02 -0.618791E-01 -0.125471E+03 0.546606E+02 0.000000E+00 0.000000E+00 0.000000E+00





Last edited on
Topic archived. No new replies allowed.