×

Discussion Board

Results 1 to 4 of 4

Thread: Binary files

  1. #1
    Regular Contributor
    Join Date
    Feb 2009
    Posts
    231

    Question Binary files

    Hi,

    What is the correct way to write information to binary files?

    I have this code, but it doesn't use QFile:

    Code:
    #include <fstream>
    ...
    struct test
    { 
    int id;
    QString text;
    }
    ...
    ofstream file ("file.dat",ios::out|ios::binary); 
    file.write(reinterpret_cast<char*>(&temporal_test),sizeof(test)); /*Temporal test is where I have saved the information and its a "test" struct */ 
    file.close();
    Regards.
    Last edited by symbi4n_programmer; 2012-04-13 at 08:11.

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: Binary files

    hmm may be QDataStream can help you to write data to binary files.

  3. #3
    Regular Contributor
    Join Date
    Feb 2009
    Posts
    231

    Re: Binary files

    Hi again,

    I have implemented this two methods:

    Code:
    struct MyStruct
    {
        int id;
        QString text;
    };
    Code:
    void MainWindow::writeFile()
    {
        QFile file("file.bin");
        if (!file.open(QIODevice::WriteOnly)) {
            QMessageBox::warning(this, tr("File creation"), tr("Unable to open file to write."));
            return;
        }
        MyStruct ms;
        ms.id = 5;
        ms.text = "abc";
        file.write((char*)&ms,sizeof(ms));
        file.close();
    }
    Code:
    void MainWindow::readFile()
    {
        QFile file("file.bin");
        if (!file.open(QIODevice::ReadOnly)) {
          QMessageBox::warning(this, tr("File creation"), tr("Unable to open file to read."));
          return;
        }
    
        MyStruct ms;
        file.read((char*)&ms, sizeof(ms));
        QMessageBox::information(this, tr("File information"), tr("id: %1, text: %2").arg(ms.id).arg(ms.text));
        file.close();
    }
    I get this in the message box:

    id: 5, text: \\?\
    What's wrong?

    Regards.

  4. #4
    Regular Contributor
    Join Date
    Feb 2009
    Posts
    231

    Re: Binary files

    I have updated my methods and now works properly:

    Code:
    void MainWindow::writeFileByQDataStream()
    {
        QFile file("file.bin");
        if (!file.open(QIODevice::WriteOnly)) {
            QMessageBox::warning(this, "File creation", "Unable to open file to write.");
            return;
        }
        QDataStream out(&file);
        MyStruct ms = { 5, "abcABCa" };
        MyStruct ms2 = { 7, "ro" };
        MyStruct ms3 = { 67, "qwerty" };
        out << (quint32)ms.id;
        out << ms.text;
        out << (quint32)ms2.id;
        out << ms2.text;
        out << (quint32)ms3.id;
        out << ms3.text;
        file.close();
    }
    Code:
    void MainWindow::readFileByQDataStream()
    {
        QFile file("file.bin");
        if (!file.open(QIODevice::ReadOnly)) {
          QMessageBox::warning(this, "File creation", "Unable to open file to read.");
          return;
        }
    
        QDataStream in(&file);
        MyStruct ms = { 0, "" };
        MyStruct ms2 = { 0, "" };
        MyStruct ms3 = { 0, "" };
        quint32 id;
        quint32 id2;
        quint32 id3;
        in >> id >> ms.text >> id2 >> ms2.text >> id3 >> ms3.text;
        ms.id = (int)id;
        ms2.id = (int)id2;
        ms3.id = (int)id3;
        QMessageBox::information(this, "File information", QString("id: %1, text: %2").arg(QString::number(ms.id)).arg(ms.text));
        QMessageBox::information(this, "File information", QString("id: %1, text: %2").arg(QString::number(ms2.id)).arg(ms2.text));
        QMessageBox::information(this, "File information", QString("id: %1, text: %2").arg(QString::number(ms3.id)).arg(ms3.text));
        file.close();
    }

Similar Threads

  1. POST multipart query for upload binary files
    By ababo in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2009-10-10, 18:58
  2. Upload Binary Files
    By somnathbanik in forum Symbian
    Replies: 3
    Last Post: 2009-09-15, 15:14
  3. Which binary was it? Resolving the binary from a stack frame
    By cjj1 in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2008-06-06, 12:40
  4. opening files in binary format
    By olympio in forum Open C/C++
    Replies: 1
    Last Post: 2007-04-11, 18:59
  5. How to read binary large binary file from...
    By gaudeamus1313 in forum Mobile Java General
    Replies: 5
    Last Post: 2007-03-20, 19:17

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×