So I have a text file with 3 different components of velocity, as well as the time stamp associated with each velocity measurement. The text file can be seen here.
The u component corresponds to the x direction, v to the y direction, and w to the z direction. Now, I need to take these data points and place them into my structure for the velocity components, which is defined as:
struct cmpnts
{
double x, y, z;
}
In the past when I did this without structures and with only one overall velocity, I just used pointers so the dynamic arrays could be created when the file was read. This has to be dynamic because the number of points in each windspeed file changes, and I can't manually redefine the value every time I use a new file.
To do this without the structures my code looked like this:
int main()
{
int numberofpoints; // the number of data points
// char arrays for storing the variables names
char ch1[128], ch2[128];
cout << ch1 << endl;
// Two pointers for creating the dynamic arrays later
double *itime, *windspeed;
// create an object for reading a file
ifstream imyfile;
// open windspeed.txt
imyfile.open("windspeed.txt");
if (imyfile.is_open()) // check if the file is open
{
// read the total number of data points
imyfile >> numberofpoints;
// double arrays for storing time and the velocity variables
itime = new double[numberofpoints];
windspeed = new double[numberofpoints];
// read the two variable names in windspeed.txt file
imyfile >> ch1 >> ch2;
// read the time and wind speed
int i;
for (i = 0; i<numberofpoints; i++)
{
imyfile >> itime[i] >> windspeed[i];
}
// close the file
imyfile.close();
}
else
{
cout << "unable to open the file";
exit(0);
}
}
I can't seem to get this to work with my structures though. I'm certain I'm making some syntax error somewhere with the pointers (I'm new to C++ so I apologize if it's something dumb!). Is it possible to do this without pointers? My code for reading to the structures looks like this (and obviously it's not working!):
#include <iostream>
#include <fstream>
using namespace std;
struct cmpnts
{
double x, y, z;
};
struct wind
{
cmpnts velocity;
cmpnts direction;
cmpnts urms;
double airdensity;
};
struct turbine
{
double R, Cp, V, yaw, power;
cmpnts direction;
};
int main()
{
// Read data from file
int numberofpoints; // the number of data points
char ch1[128], ch2[128], ch3[128], ch4[128]; // Char arrays for storing the variables names
// Pointers for creating the dynamic arrays later
double *itime;
cmpnts *uSpeed;
cmpnts *vSpeed;
cmpnts *wSpeed;
// create an object for reading a file
ifstream imyfile;
// open windspeed.txt
imyfile.open("windspeed.txt");
if (imyfile.is_open()) // check if the file is open
{
// read the total number of data points
imyfile >> numberofpoints;
// double arrays for storing time and the velocity variables
itime = new double[numberofpoints];
uSpeed->x = new double[numberofpoints];
vSpeed->y = new double[numberofpoints];
wSpeed->z = new double[numberofpoints];
// read the two variable names in windspeed.txt file
imyfile >> ch1 >> ch2 >> ch3 >> ch4;
// read the time and wind speed
int i;
for (i = 0; i<numberofpoints; i++)
{
imyfile >> itime[i] >> uSpeed[i] >> vSpeed[i] >> wSpeed[i];
}
// close the file
imyfile.close();
}
else
{
cout << "unable to open the file";
exit(0);
}
}
cmpnts *uSpeed;does?