Important: Please read the Qt Code of Conduct -

Big problem in reading&writing binary files

  • hi,
    i have a big problem in qt, assume that i use binary files and write data to them.
    my data are strings,
    and i want them string (in output mode) whenever i read them.
    any help will be appreciated :)
    thanks in advance.

  • Hi,
    On your QFile pointer you could set a QByteArray and check out that class to define how do you need that data from the file. The fromHex en toHex jump to mind in your case.

  • Thanks for your reply,
    look, this is my code :

    for writing:

    QString strToWrite = ui->User_signUp->text()+ui->Pass_signUp->text();
    QFile fileToWrite("users.dat");
    QDataStream dataStreamWriter(&fileToWrite);;
    dataStreamWriter << strToWrite;


    and for reading:

    QString strToRead= "";
    QFile fileToRead("users.dat");
    QDataStream dataStreamReader(&fileToRead);;
    dataStreamReader >> strToRead;

        qDebug() << strToRead;


    but in this case problem is that qDebug does'nt work.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Is it a typo or are you really writing to users.bin and reading from file.bin ?

  • Sorry i change the code with QByteArray,

    for writing:


    QByteArray ar=ui->User_signUp->text().toUtf8()+"%"+ ui->Pass_singUp->text().toUtf8()+"$";
    QFile file("users.dat"); | QIODevice::Append );


    this code writes the usernames and passwords correctly

    Now assume that i want to search a specified String(username) in my file, I think i can do this by using 2 QByteArrays, one for the username (which user has entered) and another for the file which reads whole file.
    but i don't know how i can use them. could you help me?

  • Lifetime Qt Champion

    Are you thinking about QByteArray::contains ?

Log in to reply