[Solved] Error with Qtextstream And Bool



  • @{
    QString srch,cr;
    QFile mys("e:/mystock.txt");
    if(mys.open(QIODevice::ReadOnly | QIODevice::Text)){
    QTextStream out(&mys);
    bool found = false;
    while(out>>names){
    if(srch==names)
    {
    ui->groupBox_3->show();
    found = true;

    }
                   if(!found){
                       QMessageBox msg;
                       msg.setText("Company Not Found");
                       msg.exec();
    

    }
    }
    }
    }@
    says:
    error: could not convert 'out.QTextStream::operator>>((* & names))' from 'QTextStream' to 'bool'
    while(out>>names){
    ^



  • out>>names does not evaluate to Boolean. It is QTextStream and does not get evaluate to bool. You can't put this as condition check. What is your intent to put this check ?



  • in another function program will write some info on the txt file in this i want to check if the info that you enter is correct the program will continiue here is full code And thanks for your replay
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>
    #include <QMessageBox>
    using namespace std;
    QString names;
    QFile mys("e:/mystock.txt");
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->groupBox_3->hide();

    }

    MainWindow::~MainWindow()
    {
    delete ui;

    }

    void MainWindow::on_pushButton_clicked()
    {
    QString name;
    int d,m,sd,amount;

    name=ui->lineEdit_name->text();
    amount=ui->lineEdit_amount->text().toInt();
    d=ui->lineEdit_d->text().toInt();
    m=ui->lineEdit_m->text().toInt();
    sd=ui->lineEdit_y->text().toInt();
    
    if(mys.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
        QTextStream input(&mys);
         names=name;
    input<<names<<" "<<amount<<"    "<<d<<"/"<<m<<"/"<<sd;
    

    }

    }

    void MainWindow::on_pushButton_2_clicked()
    {
    QString srch,cr;
    if(mys.open(QIODevice::ReadOnly | QIODevice::Text)){
    QTextStream out(&mys);
    bool found = false;
    while(out>>names){
    if(srch==names)
    {
    ui->groupBox_3->show();
    found = true;

    }
                   if(!found){
                       QMessageBox msg;
                       msg.setText("Company Not Found");
                       msg.exec(&#41;;
    

    }

    }
    }
    }

    void MainWindow::on_actionExit_triggered()
    {
    close();
    }@
    is there any other way to check?



  • I'm not sure why are using the file store and check this logic. By looking at the logic, you would like to check on the all the parameter entered ? Hope it is correct. You can use QFile.readLine. Split them in the form of tokens using QString.split. This returns the tokens in the form of StringList. You can iterate over each of them and check it.

    You can try something like the below.

    @ QFile file("my.dat");
    file.open(QIODevice::ReadOnly);
    QString line = file.readLine();
    QStringList tokens = line.split(" ");
    int count = tokens.size();
    for(int i=0;i<count;i++){
    QString token = tokens.at(i);
    qDebug() << "Token=" << token;
    }@



  • unfortunately no i only want to check one of them like
    if the .txt file be like this
    ramin 100 8/8/2012
    i only want check is ramin or not :)
    thanks



  • In that case

    @QString name = tokens.at(0)
    if (name.compare("ram in") == 0 )
    exist
    else
    does not exist@



  • thank u for your quick reply


Log in to reply
 

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