[Solved] Help needed in using qstringlist with qfile and qbytearray !!!



  • Hi everyone, am a newbie to Qt Development. Actually I wanted to develop an application with the below requirement.

    Copying file contents which consists of some integer id's like 2201, 2255, 2386 etc. to a qstringlist and comparing each single element with a qbytearray function i.e qbytearray::contains. Can anyone of you help me with this please.

    I have written some code below with no error but couldn't get output, can't understand where the problem is.

    main.cpp file
    @
    #include "tcpsocket.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    tcpSocket w;
    w.show();

    // tcpSocket tcpsocket;
    // tcpsocket.Connect();

    return a.exec&#40;&#41;;
    

    }
    @

    demo.cpp file
    @#include "demo.h"
    #include "ui_demo.h"
    #include <QTcpSocket>
    #include <QAbstractSocket>
    #include <QFile>
    #include <QString>
    #include <QTextCodec>
    #include <QLabel>

    QFile ids("/home/chocky/Desktop/ids.txt");
    QFile ids_list("/home/chocky/Desktop/ids_list.txt");

    qint16 list_size,i;

    demo::demo(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::demo)
    {
    QString file;
    ids.open(QIODevice::ReadWrite | QIODevice::Text);
    file = QTextCodec::codecForMib(1015)->toUnicode(ids.readAll());
    QStringList list = file.split(".",QString::SkipEmptyParts);
    list_size=list.size();
    ui->label->setText(QString::number(list_size));
    ids.close();

    ui->label_2->setText(list.at(1).toUtf8());
    

    }

    demo::~demo()
    {
    delete ui;
    }
    @

    Application Output:
    Starting /home/chocky/Desktop/QtProjects/build-socket-Desktop_Qt_5_3_GCC_64bit-Debug/socket...
    The program has unexpectedly finished.
    /home/chocky/Desktop/QtProjects/build-socket-Desktop_Qt_5_3_GCC_64bit-Debug/socket crashed

    Compile Output:
    19:12:03: Running steps for project socket...
    19:12:03: Configuration unchanged, skipping qmake step.
    19:12:03: Starting: "/usr/bin/make"
    make: Nothing to be done for `first'.
    19:12:03: The process "/usr/bin/make" exited normally.
    19:12:03: Elapsed time: 00:00.



  • What is the content of file ? Can you paste it ? Any reason you are doing unicode etc ? Here either file or list is null. If your intent is just read and split according to ",", you can read the contents using readLine() and split the string using split function.



  • Thanks for the reply Dheerendra, my ids.txt file contents are mentioned below.

    ids.txt
    @2103, 2204, 2296, 7485, 9375@

    As I have mentioned previously am a newbie to this c++ coding and all can you give any info regarding these unicode, binary and ascii etc., please.



  • So your error is very clear then. Look at your split. You are doing split through "."(dot). You should split your line through ","(comma)

    Try the following two lines. Replace your split @line 22 with following two lines.

    @ qDebug() << " Line =" << file <<endl;
    QStringList list = file.split(",",QString::SkipEmptyParts);@

    After this, let me know what happens.



  • Thanks Dheerender it worked amazing. Actually mine was a typo error from typing ',' I typed a '.' . So now the issue is solved thanks alot. :)



  • cool. Move this issue to SOLVED state by modifying the subject line of this issue.


Log in to reply
 

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