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.
    Greetz



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

    for writing:
    [code]

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

    [/code]

    and for reading:

    [code]
    QString strToRead= "";
    QFile fileToRead("users.dat");
    QDataStream dataStreamReader(&fileToRead);
    fileToRead.open(QIODevice::ReadOnly);
    dataStreamReader >> strToRead;
    fileToRead.close();

        qDebug() << strToRead;
    

    [/code]

    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:

    [code]

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

    [/code]

    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
 

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