Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Choose a file read it and put it into variable.
Forum Updated to NodeBB v4.3 + New Features

Choose a file read it and put it into variable.

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 709 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    serj90
    wrote on last edited by
    #1

    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&#40;&#41;;
    

    }@

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved