问题
I'm trying to read data from a text file called fields.txt
which holds the members of the struct Fields
.
{1, 0, 7.4, 39.5, 5.33784},
{3, 1, 4.6, 27.9, 6.06522},
{5, 2, 2.2, 12.5, 5.68182},
{8, 0, 14.5, 86, 5.93103},
{11, 1, 8, 43.8, 5.475},
{16, 2, 5.9, 37.7, 6.38983},
{22, 0, 12.7, 72, 5.66929},
{24, 1, 10.5, 63.2, 6.01905}
I want my program to read the data into my array of struct called Fields fielddata[8] = {};
so that I am able to use the data to create a display.
#include<iostream>
#include<fstream>
using namespace std;
std::ifstream infile("fields.txt");
int initialise(int field, int crop, float size, float yof, float yph);
struct Fields {
int Field;
int Crop;
float Size;
float Yof;
float Yph;
int initialise(int field, int crop, float size, float yof, float yph)
{
Field = field;
Crop = crop;
Size = size;
Yof = yof;
Yph = yph;
};
};
int main() {
Fields fielddata[8];
ifstream file("fields.txt");
if(file.is_open())
{
int a, b, i = 0;
float c, d, e;
while (infile >> a >> b >> c >> d >> e)
{
fielddata[i].Field = a;
fielddata[i].Crop = b;
fielddata[i].Size = c;
fielddata[i].Yof = d;
fielddata[i].Yph = e;
++i;
}
}
int highyph = 0;
cout << "Field\t" << "Crop\t" << "Size\t" << "YOF\t" << "YPH\t" << endl;
for (int i = 0; i < 8; i++) {
cout << fielddata[i].Field << "\t" << fielddata[i%3].Crop << "\t" << fielddata[i].Size << "\t" << fielddata[i].Yof << "\t" << fielddata[i].Yph << "\t" << endl;
}
for (int i = 0; i < 8; i++)
{
if (fielddata[i].Yph > highyph)
highyph = fielddata[i].Field;
}
cout << "The Field with the Highest Yield is " << highyph << endl;
system("Pause");
return 0;
}
回答1:
Edit: To specifically deal with the type of input shown in OP's post (comma delimiters with curly braces outside), this is what is done. Idea taken from this thread
//Get each line and put it into a string
String line;
while (getline(infile, line)) {
istringstream iss{regex_replace(line, regex{R"(\{|\}|,)"}, " ")};
vector<float> v{istream_iterator<float>{iss}, istream_iterator<float>{}};
//Assigns each member of the struct to a member of the vector at the relevant position
fielddata[i].Field = static_cast<int>(v.at(0));
fielddata[i].Crop = static_cast<int>(v.at(1));
fielddata[i].Size = v.at(2);
fielddata[i].Yof = v.at(3);
fielddata[i].Yph = v.at(4);
++i;
}
Basically what's happening here is:
- The program reads a line from the file and puts it into the
String line
(until there are no more lines to be read [EOF]). - An
inputstringstream
replaces all occurences of commas and curly braces with spaces, for easy acquisition. - We then use a vector to get all the numbers left over in
iss
. - Each member of the
struct fielddata
is then given the value in each relevant position in the vector. We convert the first two to integers since the vector is of typefloat
.
From this thread
First, make an
ifstream
:#include <fstream> std::ifstream infile("thefile.txt");
Assume that every line consists of two numbers and read token by token:
int a, b; while (infile >> a >> b) { // process pair (a,b) }
You'll simply need to create 5 variables which match the data types you're looking for to put into each struct. E.g. 2 int
s, 3 float
s. Then you simply follow the format outlined above and assign each variable to each member of the struct.
Also, it would be advisable to initialize all your variables at the start of main and not in the middle.
And one more bit of help to nudge you along.
int a, b, i = 0;
float c, d, e;
while (infile >> a >> b >> c >> d >> e)
{
fieldData[i].Field = a;
//Assign other struct members as you wish
++i; //Or use an inner for loop to do the incrementation
}
If you need further guidance with this let me know, but I'd like to see what you manage to do with it.
来源:https://stackoverflow.com/questions/35260662/how-do-i-read-data-from-a-text-file-into-an-array-of-struct