c++ - How do I read data from a text file into an array of struct -
i'm trying read data text file called fields.txt
holds members of 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 program read data array of struct called fields fielddata[8] = {};
able use data create 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, = 0; float c, d, e; while (infile >> >> 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; (int = 0; < 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; } (int = 0; < 8; i++) { if (fielddata[i].yph > highyph) highyph = fielddata[i].field; } cout << "the field highest yield " << highyph << endl; system("pause"); return 0; }
edit: deal type of input shown in op's post (comma delimiters curly braces outside), done. idea taken this thread
//get each line , put 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 struct member of vector @ 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 line file , puts
string line
(until there no more lines read [eof]). - an
inputstringstream
replaces occurences of commas , curly braces spaces, easy acquisition. - we use vector numbers left on in
iss
. - each member of
struct fielddata
given value in each relevant position in vector. convert first 2 integers since vector of typefloat
.
from this thread
first, make
ifstream
:#include <fstream> std::ifstream infile("thefile.txt");
assume every line consists of 2 numbers , read token token:
int a, b; while (infile >> >> b) { // process pair (a,b) }
you'll need create 5 variables match data types you're looking put each struct. e.g. 2 int
s, 3 float
s. follow format outlined above , assign each variable each member of struct.
also, advisable initialize variables @ start of main , not in middle.
and 1 more bit of nudge along.
int a, b, = 0; float c, d, e; while (infile >> >> b >> c >> d >> e) { fielddata[i].field = a; //assign other struct members wish ++i; //or use inner loop incrementation }
if need further guidance let me know, i'd see manage it.
Comments
Post a Comment