[Interest] [SOLVED] Binary file embedded in Resources to std::istream

Rollastre Prostrit rollastre at gmail.com
Mon Aug 5 21:08:45 CEST 2013


Ok, I'm not sure what the problem was but I have this and it works now 
-in case somebody is interested:


     QString fName = QString::fromStdString(filename);
     QFile f(fName);

     if (!f.exists())
         return;

     int s = f.size();
     if (s <= 0)
         return;

     if (!f.open(QIODevice::ReadOnly))
         return;

     if (!f.isReadable())
         return;

     QByteArray bytes = f.readAll();

     char* bb = bytes.data();
     std::string str;
     for (int i=0; i<f.size(); ++i)
     {
         str += bb[i];
     }

     std::stringstream stream;
     stream << str;


On 08/02/2013 10:05 PM, Karl Ruetz wrote:
>
> I'd try QDataStream.
>
> For input:
>
> QFile file("file.xxx");
> file.open(QIODevice::ReadOnly);
> QDataStream in(&file);
>
> use >> operator or readBytes method to read
>
> for output:
>
> QFile file("file.xxx");
> file.open(QIODevice::WriteOnly);
> QDataStream out(&file);
>
> use << operator or writeBytes() method to write.
>
> Karl
>
> On 2013-08-02 13:18, Rollastre Prostrit wrote:
>
>> Hi Qters:
>>
>> Due to vicissitudes of a third party library I'm using I have to read
>> the bytes of png files (binary files) embedded in the resources via
>> using a std::istream. I have been struggling quite a bit with this and
>> googling a lot without success. Maybe this is something that it is easy
>> to solve but I haven't succeeded with it yet.
>>
>> I use this snippet
>>
>>       QFile f(":/images/file.png");
>>
>>       if (!f.exist())
>>           return;
>>
>>       if (f.size()<=0)
>>           return;
>>
>>      if (!f.open(QIODevice::ReadOnly))
>>           return;
>>
>>       if (!f.isReadable())
>>           return;
>>
>>       QByteArray bytes = f.readAll();
>>
>>       char* bb = bytes.data();
>>       std::string str;
>>       for (int i=0; i<s; ++i)
>>       {
>>           str += bb[i];
>>       }
>>
>>       stringstream stream;
>>       stream << str;
>>
>>
>> I use strings here because some of the tens of articles I've read
>> suggested it. It doesn't seem to work, though. If I dump the bytes to a
>> temp file, I see the file becomes corrupted. There is an extra empty
>> byte right after the PNG header. My guess is that the zero-end byte is
>> messing everything up. But I admit that I don't find any solution for this.
>>
>> Has somebody faced this problem and/or can see what/how is the solution?
>> Thanks in advance!
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org  <mailto:Interest at qt-project.org>
>> http://lists.qt-project.org/mailman/listinfo/interest
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130805/51e15bf0/attachment.html>


More information about the Interest mailing list