reading file ,searching word and storing the values corresponding to that word into arrays

Dec 22, 2015 at 3:02pm
hey guys i have a file in following form

TIME= 0.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282

TIME= 2.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282


TIME= 4.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282


TIME= 6.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282


TIME= 8.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282

TIME= 10.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282

i need a program which takes a input value(i.e time)and search the given time in the file.Further after finding the given time ,it stores the values corresponding to given time in three different arrays.
I have written the program for this list :

-0.65043 6.35235 -0.01081 -0.00014 -0.00981 0.00329
-0.63086 6.16032 -0.01083 -0.00028 -0.00976 0.00657
-0.61129 5.96829 -0.01086 -0.00034 -0.00969 0.00967
-0.59172 5.77623 -0.01089 -0.00038 -0.00961 0.01154
-0.57215 5.58411 -0.01096 -0.00035 -0.00945 0.01071
-0.55258 5.39199 -0.01103 -0.00034 -0.00929 0.00992



[#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;
double interpolation (double x1, double x2, double y1, double y2, double xInt)
{

double yInt = y1 + (y2-y1)/(x2-x1)*(xInt-x1);

return yInt;
}
double U (double z)
{
ifstream theFile("vel.txt");
double depth;
int n=0;
int f=0;
double y[100]={0};

double v[100]={0};


double velocity;

double pressure;
double velocityx;
double velocityy;
double acclx;
double accly;
while (theFile>>depth>>pressure>>velocityx>>velocityy>>acclx>>accly)
{
y[n]=depth;

v[n]=velocityx;

n=n+1;
}

for(int i=0;i<n;i++)
{
if(y[i]==z)
{
velocity=v[i];

}
else if(y[i]<z)
{
f=f+1;
}
else
{
velocity=interpolation (y[f-1], y[f], v[f-1], v[f], z);

break;
}
}
return velocity;
}
int main()
{
double z;
cin>>z;
cout<<"velocity in x direction is "<<U(z);
return 0;


}]
Last edited on Dec 22, 2015 at 3:04pm
Dec 22, 2015 at 4:01pm
I have written the program for this list :

-0.65043 6.35235 -0.01081 -0.00014 -0.00981 0.00329
-0.63086 6.16032 -0.01083 -0.00028 -0.00976 0.00657
-0.61129 5.96829 -0.01086 -0.00034 -0.00969 0.00967
-0.59172 5.77623 -0.01089 -0.00038 -0.00961 0.01154
-0.57215 5.58411 -0.01096 -0.00035 -0.00945 0.01071
-0.55258 5.39199 -0.01103 -0.00034 -0.00929 0.00992

How does that list correspond to your input file:
TIME= 8.00
9.00000 -0.67000 6.57270
9.00000 -0.65029 6.37938
9.00000 -0.63059 6.18606
9.00000 -0.61088 5.99274
9.00000 -0.59117 5.79942
9.00000 -0.57147 5.60610
9.00000 -0.55176 5.41278
9.00000 -0.53206 5.21946
9.00000 -0.51235 5.02614
9.00000 -0.49264 4.83282




Dec 23, 2015 at 5:14am
hey jlb,
thanx for reply.
the list i have shown for which i have written the code is just an example.
can u please help me in writing the required code??
I m not able to search and store the values for aparticular time step.
Thanx in advane
Dec 23, 2015 at 5:37am
How you read the file depends on the format of the file and what you intend to do with the data contained within the file. You should start by designing a data structure to hold the data in the file.

But no, I won't write the code for you. If I did you wouldn't learn anything. Besides I really don't know what each line of your file represents and without knowing this just reading the data is useless.


Topic archived. No new replies allowed.