Choose a file read it and put it into variable.
-
Hello to everybody.
What I'm trying to do is to open a window with push button choose a file that contains 2d array and then read in to a variable [][].I found out how to do it separately, but all together doesn't work.
If you could give me an advice or example, I would be very thankful.
That's how I read the file:
@void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Introdu fisierul cu datele Companiilor "));
if(fileName.isEmpty()){QMessageBox::warning(this, tr("Introducerea fisierului"),
tr("Nu ati introdus nici un fisier "));
return; }QFile file(fileName); if(!file.open(QFile::ReadOnly |QFile::Text)){ QMessageBox::warning(this, tr("Atentie!!!"), tr("Nu se deschide fisierul %1:\n%2.") .arg(fileName) .arg(file.errorString())); return; } QTextStream in(&file); if(in.atEnd()){QMessageBox::warning(this, tr("Atentie!!!"), tr("Fisierul dat:\n %1\n este NULL.") .arg(fileName));} for(int i=0;i<L;i++) { for(int j=0;j<C;j++) { in>>matrice[i][j]; } } QMessageBox::information(this,"Information","Successful Load of data"); file.close(); }@
Here I'm trying to count columns and rows :
@typedef vector<double> ROW;
bool parse_header_row(const string & line, vector<string> &labels)
{ stringstream ss(line);while(! ss.eof()) { string lbl; if (!getline(ss, lbl, ' ')) return false; // some error labels.push_back(lbl); } return true;
}
bool parse_data_row(const string & line, vector<ROW> & vdata)
{ stringstream iss(line);
string cell;
int val;
ROW row;while (!iss.eof()) { // good status, get one cell if (!getline(iss, cell, ' ')) return false; // some error stringstream convertor(cell); convertor >> val; // Input double from cell row.push_back (val); // Add value to row } vdata.push_back (row); // Add row to vector of rows C=row.size(); L++; return true; // end of line
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();//vector<string> labels; vector<ROW> vdata; string line; fstream myfile1; myfile1.open (fileName); while (getline(myfile1, line)) { if (!parse_data_row(line, vdata)) { // handle the error return 0; } } myfile1.close(); return a.exec();
}@
array ex:
0 0 0 0 1 0 0 1
0 0 1 0 1 0 0 1
0 1 0 0 1 0 0 1
0 1 1 0 1 0 0 1
1 1 0 0 1 0 0 1
1 1 1 0 1 0 0 1
0 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0
1 1 0 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 1
1 1 1 1 1 1 1 0
0 0 1 0 0 0 1 1
0 0 0 0 0 0 1 1
0 1 0 0 0 0 1 0
0 1 1 0 0 0 1 0
0 1 0 0 0 0 1 1
0 1 1 0 0 0 1 1
1 1 0 0 0 0 1 1
0 0 0 1 1 1 1 0
0 1 1 1 1 1 1 0