[INVALID] String is not read from a text file



  • Hello. I have created a dialog box to open a path to a file on my computer and then save this file in a property file. I will then use this path to open a database. What I am also trying to do is to have the current path saved in the property file automatically appear in the textbox of the dialog. This is what I have:

    [code]

    #include "opennavdata.h"
    #include "ui_opennavdata.h"
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include <QTextStream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <iostream>

    OpenNavdata::OpenNavdata(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OpenNavdata)
    {
    ui->setupUi(this);

    // Opening the nav database from the provided path
    std::ifstream ifs("Airports.txt");
    
    // we need to look a line starting with "navdata.path="
    const std::string search_string = "navdata.path" ;
    
    std::string line;
    std::string value;
    QStringList tokens;
    
    while( std::getline( ifs, line ) && line.find(search_string) != 0 );
    
        if( line.find(search_string) != 0 )
        {
            ui->navDataDialogTextLine->setText("NavData is not selected");
        }
    
        else
        {
            value = line + '\n' ;
            QString qvalue = QString::fromStdString(value);
            tokens = qvalue.split("=");
            QString final;
            final = tokens[1];
            ui->navDataDialogTextLine->setText(final);
        }
    

    }

    OpenNavdata::~OpenNavdata()
    {
    delete ui;
    }

    // Button used to locate the Airports.txt navdata file
    void OpenNavdata::on_butNavdataDialogBrowse_clicked()
    {
    navDataFilePath = QFileDialog::getOpenFileName(this, tr("Open Airports.txt file"), "/", tr("Airports.txt"));
    ui->navDataDialogTextLine->setText(navDataFilePath);
    }

    // Button used to store the path of the Airports navdata file in the property file
    void OpenNavdata::on_butNavdataDialogOK_clicked()
    {
    QString doc;
    doc = "navdata.path=" + navDataFilePath + "\n";
    QFile dataFile("igodispatch.property");

    if (dataFile.open(QFile::WriteOnly | QFile::Truncate&#41;) {
         QTextStream out(&dataFile);
         out << doc;
    }
    
    dataFile.close();
    close();
    

    }

    // Button to close the navdata dialog box
    void OpenNavdata::on_butNavdataDialogCancel_clicked()
    {
    close();
    }

    [/code]

    However, when I open the dialog, the textbox has the following "NavData is not selected". I cannot understand what I am doing wrong. Does it even open the file?

    THANKS!

    Igor



  • EDIT: Sorry!!! I just realized that I was opening the wrong file. Please disregard! (Sorry, no option to delete the message). :-)


  • Moderators

    [quote author="igorland" date="1383095304"]EDIT: Sorry!!! I just realized that I was opening the wrong file. Please disregard! (Sorry, no option to delete the message). :-)[/quote]That's ok :) Glad to hear you fixed your issue.

    You can edit your original post to make a note at the top, so that people don't start reading your code. Even better, edit your title and add "[INVALID]" to the front.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.