Also habe ich eine Textdatei mit 3 verschiedenen Komponenten der Geschwindigkeit, sowie den Zeitstempel für jede Geschwindigkeitsmessung. Die text file can be seen here.Komponente Vektordaten aus Textdatei in Strukturarray lesen C++
Die Komponente u entspricht der x-Richtung, v der y-Richtung und w der z-Richtung. Nun, ich brauche diese Datenpunkte zu nehmen und sie in meine Struktur für die Geschwindigkeitskomponenten setzen, die wie folgt definiert ist:
struct cmpnts
{
double x, y, z;
}
In der Vergangenheit, als ich das tat, ohne Strukturen und mit nur einer Gesamtgeschwindigkeit, ich habe gerade verwendete Zeiger, damit die dynamischen Arrays beim Lesen der Datei erstellt werden konnten. Dies muss dynamisch sein, da sich die Anzahl der Punkte in jeder Windgeschwindigkeitsdatei ändert, und ich kann den Wert nicht jedes Mal neu definieren, wenn ich eine neue Datei verwende.
dies mein Code ohne die Strukturen zu tun sah wie folgt aus:
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);
}
}
ich nicht dieses zwar mit meinen Strukturen zu arbeiten zu bekommen scheinen. Ich bin sicher, dass ich irgendwo einen Syntaxfehler mit den Zeigern mache (ich bin neu in C++, also entschuldige ich mich, wenn es etwas dummes ist!). Ist es möglich, dies ohne Zeiger zu tun? Mein Code für den Strukturen zu lesen wie folgt aussieht (und offensichtlich ist es nicht funktioniert!):
#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);
}
}
Was stellen Sie sich vor das 'cmpnts * uSpeed;' tut? – kfsone
Bitte zeigen Sie auch, wie windspeed.txt tatsächlich aussieht. – kfsone